Use group by 2

Source: Internet
Author: User

We often encounter the problem of using group by using LINQ. Here we will introduce how to solve the problem of using group by using LINQ.

1. 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()  
  7. }; 

Statement Description: Use group by and count to obtain the number of products in each categoryid.

Note: category by categoryid is used to retrieve the categoryid value and the quantity of products under each category.

2. 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: Use group by and count to obtain the number of out-of-Stock Products in each categoryid.

Note: first, sort by categoryid to get the categoryid value and the number of out-of-stock items of each category. In the count function, lambda expressions are used. P in lambda expressions represents an element or object in the group, that is, a product.

3. Where restrictions

 
 
  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: queries IDs and product quantities with the product quantity greater than 10 based on the product ID group. In this example, after the group by clause, use the WHERE clause to find all categories of at least 10 products.

Note: when translated into SQL statements, the where condition is nested in the outermost layer.

4. 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.

Note: products and suppliers are classified. After by, an anonymous class is created. Here, the key is actually a class object. The key contains two properties: categoryid and supplierid. You can use g. Key. categoryid to traverse the value of categoryid.

5. Expression)

 
 
  1. var categories =  
  2. from p in db.Products  
  3. group p by new { Criterion = p.UnitPrice > 10 } into g  
  4. select g; 

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

Note: whether the unit price is more than 10 categories. The result is divided into two types: greater than one type, smaller than or equal to the other type.

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.