In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces Linq how to use Group By, the article is very detailed, has a certain reference value, interested friends must read it!
1. Count
Var Q = from p in db.Products group p by p.CategoryID into g select new {g.Key, NumProducts = g.Count ()}
Statement description: Linq uses Group By and Count to get the number of products in each CategoryID.
Description: first classify according to CategoryID, take out the Category ID value and the number of products in each category.
two。 Conditional counting
Var Q = from p in db.Products group p by p.CategoryID into g select new {g.Key, NumProducts = g.Count (p = > p.Discontinued)}
Statement description: Linq uses Group By and Count to get the number of interrupted products per CategoryID.
Description: first classify according to CategoryID, take out the Category ID value and the out-of-stock quantity of each classified product. In the Count function, the Lambda expression is used, and the p in the Lambda expression represents an element or object in this group, that is, a product.
3.Where restriction
Var Q = from p in db.Products group p by p.CategoryID into g where g.Count () > = 10 select new {g.Key, ProductCount = g.Count ()}
Statement description: according to the-ID grouping of products, query the ID and the number of products whose number of products is greater than 10. This example uses the where clause after the Group By clause to find all categories with at least 10 products.
Note: when translated into SQL statements, Where conditions are nested on the outermost layer.
4. Multiple columns (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: not only by product classification, but also by supplier classification. After by, new comes up with an anonymous class. Here, Key is essentially an object of a class, and Key contains two Property:CategoryID and SupplierID. You can traverse the value of CategoryID with g.Key.CategoryID.
5. Expression (Expression)
Var categories = from p in db.Products group p by new {Criterion = p.UnitPrice > 10} into g select g
Statement description: Linq uses Group By to return two product sequences. * the series 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: classified according to whether the unit price of the product is greater than 10. The results are divided into two categories, one is greater than, and the other is less than and equal to.
The above is all the content of the article "how Linq uses Group By". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.