http://www.cnblogs.com/death029/archive/2011/07/23/2114877.html
1.簡單形式:
- var q =
- from p in db.Products
- group p by p.CategoryID into g
- 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.最大值
- var q =
- from p in db.Products
- group p by p.CategoryID into g
- select new {
- g.Key,
- MaxPrice = g.Max(p => p.UnitPrice)
- };
語句描述:Linq使用Group By和Max查找每個CategoryID的最高單價。
說明:先按CategoryID歸類,判斷各個分類產品中單價最大的Products。取出CategoryID值,並把UnitPrice值賦給MaxPrice。
3.最小值
- var q =
- from p in db.Products
- group p by p.CategoryID into g
- select new {
- g.Key,
- MinPrice = g.Min(p => p.UnitPrice)
- };
語句描述:Linq使用Group By和Min查找每個CategoryID的最低單價。
說明:先按CategoryID歸類,判斷各個分類產品中單價最小的Products。取出CategoryID值,並把UnitPrice值賦給MinPrice。
4.平均值
- var q =
- from p in db.Products
- group p by p.CategoryID into g
- select new {
- g.Key,
- AveragePrice = g.Average(p => p.UnitPrice)
- };
語句描述:Linq使用Group By和Average得到每個CategoryID的平均單價。
說明:先按CategoryID歸類,取出CategoryID值和各個分類產品中單價的平均值。
5.求和
- var q =
- from p in db.Products
- group p by p.CategoryID into g
- select new {
- g.Key,
- TotalPrice = g.Sum(p => p.UnitPrice)
- };
學習Linq時,經常會遇到Linq使用Group By問題,這里將介紹Linq使用Group By問題的解決方法。
1.計數
- var q =
- from p in db.Products
- group p by p.CategoryID into g
- select new {
- g.Key,
- NumProducts = g.Count()
- };
語句描述:Linq使用Group By和Count得到每個CategoryID中產品的數量。
說明:先按CategoryID歸類,取出CategoryID值和各個分類產品的數量。
2.帶條件計數
- var q =
- from p in db.Products
- group p by p.CategoryID into g
- select new {
- g.Key,
- NumProducts = g.Count(p => p.Discontinued)
- };
語句描述:Linq使用Group By和Count得到每個CategoryID中斷貨產品的數量。
說明:先按CategoryID歸類,取出CategoryID值和各個分類產品的斷貨數量。 Count函數里,使用了Lambda表達式,Lambda表達式中的p,代表這個組里的一個元素或對象,即某一個產品。
3.Where限制
- 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()
- };
語句描述:根據產品的―ID分組,查詢產品數量大於10的ID和產品數量。這個示例在Group By子句后使用Where子句查找所有至少有10種產品的類別。
說明:在翻譯成SQL語句時,在最外層嵌套了Where條件。
4.多列(Multiple Columns)
- var categories =
- from p in db.Products
- group p by new
- {
- p.CategoryID,
- p.SupplierID
- }
- into g
- select new
- {
- g.Key,
- g
- };
語句描述:Linq使用Group By按CategoryID和SupplierID將產品分組。
說明:既按產品的分類,又按供應商分類。在by后面,new出來一個匿名類。這里,Key其實質是一個類的對象,Key包含兩個Property:CategoryID、SupplierID。用g.Key.CategoryID可以遍歷CategoryID的值。
5.表達式(Expression)
- var categories =
- from p in db.Products
- group p by new { Criterion = p.UnitPrice > 10 } into g
- 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.條件 ? "是" : "否"
};
