LINQ uses GROUP by 1
1. Simple form:
- var q =
- from P in Db. Products
- Group p by P.categoryid into G
- 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
- var q =
- from P in Db. Products
- Group p by P.categoryid into G
- Select New {
- G.key,
- Maxprice = g.max (p => p.unitprice)
- };
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
- var q =
- from P in Db. Products
- Group p by P.categoryid into G
- Select New {
- G.key,
- Minprice = g.min (p => p.unitprice)
- };
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
- var q =
- from P in Db. Products
- Group p by P.categoryid into G
- Select New {
- G.key,
- Averageprice = G.average (p => p.unitprice)
- };
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
- var q =
- from P in Db. Products
- Group p by P.categoryid into G
- Select New {
- G.key,
- Totalprice = g.sum (p => P.unitprice)
- };
LINQ uses GROUP by 1