Linq使用Group By [轉]


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

1.簡單形式:

  1. var q =
  2. from p in db.Products
  3. group p by p.CategoryID into g
  4. select g;

語句描述:Linq使用Group By按CategoryID划分產品。

說明:from p in db.Products 表示從表中將產品對象取出來。group p by p.CategoryID into g表示對p按CategoryID字段歸類。其結果命名為g,一旦重新命名,p的作用域就結束了,所以,最后select時,只能select g。

2.最大值

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

語句描述:Linq使用Group By和Max查找每個CategoryID的最高單價。

說明:先按CategoryID歸類,判斷各個分類產品中單價最大的Products。取出CategoryID值,並把UnitPrice值賦給MaxPrice。

3.最小值

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

語句描述:Linq使用Group By和Min查找每個CategoryID的最低單價。

說明:先按CategoryID歸類,判斷各個分類產品中單價最小的Products。取出CategoryID值,並把UnitPrice值賦給MinPrice。

4.平均值

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

語句描述:Linq使用Group By和Average得到每個CategoryID的平均單價。

說明:先按CategoryID歸類,取出CategoryID值和各個分類產品中單價的平均值。

5.求和

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

學習Linq時,經常會遇到Linq使用Group By問題,這里將介紹Linq使用Group By問題的解決方法。

1.計數

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

語句描述:Linq使用Group By和Count得到每個CategoryID中產品的數量。

說明:先按CategoryID歸類,取出CategoryID值和各個分類產品的數量。

2.帶條件計數

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

語句描述:Linq使用Group By和Count得到每個CategoryID中斷貨產品的數量。

說明:先按CategoryID歸類,取出CategoryID值和各個分類產品的斷貨數量。 Count函數里,使用了Lambda表達式,Lambda表達式中的p,代表這個組里的一個元素或對象,即某一個產品。

3.Where限制

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

語句描述:根據產品的―ID分組,查詢產品數量大於10的ID和產品數量。這個示例在Group By子句后使用Where子句查找所有至少有10種產品的類別。

說明:在翻譯成SQL語句時,在最外層嵌套了Where條件。

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

語句描述:Linq使用Group By按CategoryID和SupplierID將產品分組。

說明:既按產品的分類,又按供應商分類。在by后面,new出來一個匿名類。這里,Key其實質是一個類的對象,Key包含兩個Property:CategoryID、SupplierID。用g.Key.CategoryID可以遍歷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; 

語句描述:Linq使用Group By返回兩個產品序列。第一個序列包含單價大於10的產品。第二個序列包含單價小於或等於10的產品。

說明:按產品單價是否大於10分類。其結果分為兩類,大於的是一類,小於及等於為另一類。

 

描述:根據顧客的國家分組,查詢顧客數大於5的國家名和顧客數
查詢句法:
var 一般分組 = from c in ctx.Customers
group c by c.Country into g
where g.Count() > 5
orderby g.Count() descending
select new
{
國家 = g.Key,
顧客數 = g.Count()
};
對應SQL:
SELECT [t1].[Country], [t1].[value3] AS [顧客數]
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]

描述:根據國家和城市分組,查詢顧客覆蓋的國家和城市
查詢句法:
var 匿名類型分組 = from c in ctx.Customers
group c by new { c.City, c.Country } into g
orderby g.Key.Country, g.Key.City
select new
{
國家 = g.Key.Country,
城市 = g.Key.City
};
描述:按照是否超重條件分組,分別查詢訂單數量
查詢句法:
var 按照條件分組 = from o in ctx.Orders
group o by new { 條件 = o.Freight > 100 } into g
select new
{
數量 = g.Count(),
是否超重 = g.Key.條件 ? "是" : "否"
};


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM