LINQ uses GROUP by 1

Source: Internet
Author: User

LINQ uses GROUP by 1

1. Simple form:

    1. var q =
    2. from P in Db. Products
    3. Group p by P.categoryid into G
    4. Select G;

Statement Description: LINQ uses Group by to divide products by CategoryID.

Description: from P in db. Products means that the product objects are taken out of the table. Group p by P.categoryid into G indicates that P is categorized by the CategoryID field. The result is named G, and once renamed, the scope of P is ended, so the last select can only select G.

2. Maximum Value

    1. var q =
    2. from P in Db. Products
    3. Group p by P.categoryid into G
    4. Select New {
    5. G.key,
    6. Maxprice = g.max (p => p.unitprice)
    7. };

Statement Description: LINQ uses GROUP by and Max to find the highest unit price per CategoryID.

Description: First according to CategoryID classification, judging the price of the largest products in each category. Remove the CategoryID value and assign the UnitPrice value to Maxprice.

3. Minimum value

    1. var q =
    2. from P in Db. Products
    3. Group p by P.categoryid into G
    4. Select New {
    5. G.key,
    6. Minprice = g.min (p => p.unitprice)
    7. };

Statement Description: LINQ uses Group by and Min to find the lowest unit price per CategoryID.

Description: First, according to CategoryID classification, judging the lowest unit price of products in each category. Remove the CategoryID value and assign the UnitPrice value to Minprice.

4. Average

    1. var q =
    2. from P in Db. Products
    3. Group p by P.categoryid into G
    4. Select New {
    5. G.key,
    6. Averageprice = G.average (p => p.unitprice)
    7. };

Statement Description: LINQ uses Group by and average to get the average unit price per CategoryID.

Description: First by CategoryID classification, remove the CategoryID value and the average price of each product category.

5. Summation

    1. var q =
    2. from P in Db. Products
    3. Group p by P.categoryid into G
    4. Select New {
    5. G.key,
    6. Totalprice = g.sum (p => P.unitprice)
    7. };

LINQ uses GROUP by 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.