Group By/Having of LINQ to SQL Statement (6)

Group By/Having of LINQ to SQL Statement (6)

Group By/Having Operator

 

Applicable scenarios: grouping data, narrowing the scope for us to find data.

 

Description: Allocate and return an enumerable object after grouping the incoming parameters. grouping; delay

 

1. Simple form:

 

var q =   from p in db.Products  group pby p.CategoryID into g   select g;

 

Statement description: Use Group By to divide products by CategoryID.

 

Description: from p in db.Products means to fetch the product object from the table. group p by p.CategoryID into g

Indicates that p is classified by CategoryID field. The result is named g, and once it is renamed, the scope of p ends, so in the last select, only g can be selected. Of course, you don't have to rename it and you can write it like this:

 

var q =   from p in db.Products  group pby p.CategoryID;

 

We use a diagram to represent:

 

 

 

If you want to iterate over all records in a category, like this:

 

foreach (var gp in q)

{

  if (gp.Key == 2)

  {

     foreach (var item in gp)

    {

      //do something

    }

  }

2.Select anonymous class:

 

var q =   from p in db.Products  group pby p.CategoryID into g   select new { CategoryID = g.Key, g };

 

Description: In this LINQ statement, there are 2 properties: CategoryID and g. This anonymous class, its essence is to return

The result set is repackaged. Encapsulate the property of g into a complete group. As shown below:

 

 

 

If you want to iterate over all records in an anonymous class, do this:

 

foreach (var gp in q)

{

  if (gp.CategoryID == 2)

  {

    foreach (var item in gp.g)

    {

       //do something

    }

  }

3. Maximum value

 

var q =   from p in db.Products  group pby p.CategoryID into g   select new {

    g.Key,

    MaxPrice = g.Max(p => p.UnitPrice)

   };

 

Statement description: Use Group By and Max to find the highest unit price of each CategoryID.

 

Description: First, classify by CategoryID, and determine the Products with the largest unit price among the products in each category. Take the CategoryID value and assign the UnitPrice value to MaxPrice.

 

4. Minimum

 

var q =   from p in db.Products  group pby p.CategoryID into g   select new {

    g.Key,

    MinPrice = g.Min(p => p.UnitPrice)

   };

 

Statement description: Use Group By and Min to find the lowest unit price of each CategoryID.

 

Description: First classify by CategoryID, and determine the Products with the smallest unit price among the products in each category. Take the CategoryID value and assign the UnitPrice value to MinPrice.

 

5. Average

 

var q =   from p in db.Products  group pby p.CategoryID into g   select new {

    g.Key,

    AveragePrice = g.Average(p => p.UnitPrice)

   };

 

Statement description: Use Group By and Average to get the average unit price of each CategoryID.

 

Description: First classify by CategoryID, and take out the CategoryID value and the average unit price of each classified product.

 

6. Summation

 

var q =

  from p in db.Products   group p byp.CategoryID into g   select new {

    g.Key,

    TotalPrice = g.Sum(p => p.UnitPrice)

  };

 

Statement description: Use Group By and Sum to get the total unit price of each CategoryID .

 

Description: First classify by CategoryID, and take out the value of CategoryID and the sum of the unit price of each classified product.

 

7. Count

 

var q =   from p in db.Products  group pby p.CategoryID into g   select new {

    g.Key,

    NumProducts = g.Count()

  };

 

Statement description: Use Group By and Count to get the number of products in each CategoryID.

 

Explanation: Classify by CategoryID first, take out the CategoryID value and the quantity of each classified product.

 

8. Conditional counting

 

var q =   from p in db.Products  group pby p.CategoryID into g   select new {

    g.Key,

    NumProducts = g.Count(p => p.Discontinued)

  };

 

Statement description: Use Group By and Count to get the number of out-of-stock products for each CategoryID.

 

Description: First classify according to CategoryID, take out the CategoryID value and the out-of-stock quantity of each classified product. In the Count function, a lambda expression is used. The p in the lambda expression represents an element or object in this group, that is, a certain

product.

 

9.Where restrictions

 

var q =   from p in db.Products  group pby p.CategoryID into g   where g.Count() >= 10   select new {

    g.Key,

     ProductCount =g.Count()

  };

 

Statement description: Group by product ―ID, query ID and product quantity whose product quantity is greater than 10. This example uses the Where clause after the Group By clause to find all categories with at least 10 products.

 

Explanation: When translating into an SQL statement, a Where condition is nested at the outermost level.

 

10. 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: Use Group By to group products by CategoryID and SupplierID.

 

Description: Both by product and by supplier. After by, new comes out an anonymous class. Here, Key is essentially an object of a class, and Key contains two Properties: CategoryID and SupplierID. Use g.Key.CategoryID to traverse the value of CategoryID.

 

11. Expression

 

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

 

Statement description: Use 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 10 or less.

 

Description: Classify according to whether the unit price of the product is greater than 10. The results are divided into two categories, greater than one category, less than and equal to the other category.


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326626241&siteId=291194637