Group By In LINQ

Source: Internet
Author: User

This article is guided by: LINQ defines about 40 query operators, such as Select, from, in, where, group, and order by, and with LINQ technology, we can query any form of data using a SQL-like syntax. LINQ has a lot to learn, here we mainly introduce LINQ using Group by.

I. who applies LINQ to

LINQ syntax is supported by System.Linq the enumerable class below, and by observing his signature, you will find that he implements a series of extension methods for ienumerable<t>, that is, as long as it is implemented IEnumerable <T> objects can be queried using LINQ syntax.

Ii. keywords in LINQ

In ASP., some new keywords were introduced for LINQ, and they were:

From join where group to let select

The students who are familiar with SQL look familiar, in fact, in LINQ they have a similar meaning in SQL, so it will be easy to understand.

Third, the following is a common scenario for LINQ to use GROUP by

1. Simple form:

  
  
  

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  
  {      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  
   {       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  
   {        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  
   {       G.key,       totalprice = g.sum (p = p.unitprice)     

6.Where limit

var q =  
   from P in Db. Products  
   Group p by P.categoryid into G  
   where G.count () >= 10  
   {       G.key,       productcount = G.count ()     

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.

7. Multi-column (multiple Columns)

var categories =  
   from P in Db. Products  
   Group p by New  
   {       P.categoryid,       P.supplierid     } into     g     Select New     {       g.key,       g     

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.

8. Expressions (expression)

var categories =  
   from P in Db. Products  
   {Criterion = p.unitprice > ten} into 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.

Transfer from http://www.studyofnet.com/news/397.html

Group By In LINQ

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.