"Go" Linq Group by

Source: Internet
Author: User

Http://www.cnblogs.com/death029/archive/2011/07/23/2114877.html

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. };

When you learn LINQ, you often encounter LINQ using the group by problem, which explains how LINQ uses the group by problem.

1. Counting

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

Statement Description: LINQ uses Group by and count to get the number of products in each CategoryID.

Description: First by CategoryID classification, remove the CategoryID value and the number of individual products.

2. With conditional count

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

Statement Description: LINQ uses Group by and count to get the number of discontinued products per CategoryID.

Description: First by CategoryID classification, remove the CategoryID value and the number of broken goods of each category. In the Count function, a lambda expression is used, and p in the lambda expression represents an element or object in the group, which is a product.

3.Where limit

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

Statement Description: Based on the product's ―id grouping, the product number is queried for more than 10 of the ID and product quantity. This example uses the WHERE clause after the GROUP BY clause to find all categories with at least 10 products.

Description: A Where condition is nested at the outermost layer when translated into an SQL statement.

4. Multi-column (multiple Columns)

    1. var categories =
    2. from P in Db. Products
    3. Group p by New
    4. {
    5. P.categoryid,
    6. P.supplierid
    7. }
    8. into G
    9. Select New
    10. {
    11. G.key,
    12. G
    13. };

Statement Description: LINQ uses Group by to group products by CategoryID and SupplierID.

Description: Both by product classification, and by supplier classification. After by, new comes out an anonymous class. Here, key is essentially a class object, and key contains two Property:categoryid, SupplierID. With G. Key.categoryid can traverse the value of CategoryID.

5. Expressions (expression)

    1. var categories =
    2. from P in Db. Products
    3. Group p by new { Criterion = p.unitprice > ten} into G
    4. Select G;

Statement Description: LINQ uses GROUP by to return two series of products. The first sequence contains products with a unit price greater than 10. The second sequence contains products with a unit price less than or equal to 10.

Description: By product unit Price is greater than 10 classification. The results are divided into two categories, greater than the one, less than and equal to another class.

Description: According to the customer's country group, query the number of customers more than 5 country name and number of customers
query syntax:
var general grouping = from C in CTX. Customers
Group C by C.country to G
where G.count () > 5
by G.count () descending
Select New
{
Country = G.key,
number of customers = G.count ()
};
Corresponding sql:
SELECT [t1].[ Country], [T1]. [Value3] As [number of customers]
from (
SELECT count (*) as [value], COUNT (*) as [value2], COUNT (*) as [value3], [t0].[ Country]
from [dbo].[ Customers] as [t0]
GROUP by [t0].[ Country]
) as [T1]
WHERE [t1].[ Value] > @p0
ORDER by [t1].[ Value2] DESC
--@p0: Input Int32 (Size = 0; Prec = 0; Scale = 0) [5]

Description: Check the country and city of customer coverage according to the country and City group
Query syntax:
var anonymous type grouping = from C in CTX. Customers
Group C by new {c.city, c.country} to G
G.key.country, g.key.city
Select New
{
Country = G.key.country,
City = g.key.city
};
Description: Check the order quantity separately according to whether the overweight condition is grouped
Query syntax:
var is grouped by condition = from O in CTX. Orders
Group O by new {condition = o.freight > +} into G
Select New
{
Quantity = G.count (),
is overweight = G.key. Conditions? "Yes": "No"
};

"Go" Linq Group by

Related Article

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.