LINQ使用之Group By使用

來源:互聯網
上載者:User

LINQ只能在VS2005以後版本使用,即VS2008,VS2010,VS2012(即.net3.0版本以上)

1.簡單形式:

var q =from p in db.Products
      group p by p.CategoryID into g
      select g;
語句描述:使用Group By按CategoryID劃分產品。
說明:from p in db.Products 表示從表中將產品對象取出來。group p by p.CategoryID into g表示對p按CategoryID欄位歸類。其結果命名為g,一旦重新命名,p的範圍就結束了,所以,最後select時,只能select g。當然,也不必重新命名可以這樣寫:
var q =from p in db.Products
      group p by p.CategoryID;

我們用表示:

如果想遍曆某類別中所有記錄,這樣:

foreach (var gp in q)
  {      if (gp.Key == 2)
      {
          foreach (var item in gp)
          {              //do something          }      }  }

2.Select匿名類:

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

說明:在這句LINQ語句中,有2個property:CategoryID和g。這個匿名類,其實質是對返回結果集重新進行了封裝。把g的property封裝成一個完整的分組。

如果想遍曆某匿名類中所有記錄,要這麼做:

foreach (var gp in q)
  {
      if (gp.CategoryID == 2)
      {
          foreach (var item in gp.g)
          {              //do something          }      }  }

3.最大值

var q =from p in db.Products
     group p by p.CategoryID into g
      select new {g.Key,MaxPrice = g.Max(p => p.UnitPrice)};

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

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

4.最小值

var q =from p in db.Products
      group p by p.CategoryID into g
      select new {g.Key,MinPrice = g.Min(p => p.UnitPrice)};

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

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

5.平均值

var q =from p in db.Products
      group p by p.CategoryID into g
      select new {g.Key,AveragePrice = g.Average(p => p.UnitPrice)};

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

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

6.求和

var q =from p in db.Products
      group p by p.CategoryID into g
      select new {g.Key,TotalPrice = g.Sum(p => p.UnitPrice)};

語句描述:使用Group By和Sum得到每個CategoryID 的單價總計。

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

7.計數

var q =from p in db.Products
      group p by p.CategoryID into g
      select new {g.Key,NumProducts = g.Count()};

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

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

8.帶條件計數

var q =from p in db.Products
      group p by p.CategoryID into g
      select new {g.Key,NumProducts = g.Count(p => p.Discontinued)};

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

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

9.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條件。

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

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

說明:既按產品的分類,又按供應商分類。在by後面,new出來一個匿名類。這裡,Key其實質是一個類的對象,Key包含兩個Property:CategoryID、SupplierID。用g.Key.CategoryID可以遍曆CategoryID的值。

11.運算式(Expression)

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

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

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

DataTable中查詢使用LINQ:

DataTable mTable=new DataTable();

......mTable中裝入資料,

var result = from row in mTable.AsEnumerable()
                         group row by new                         {
                             title = row.Field<string>("Title"),
                             module = row.Field<string>("Module")
                         } into g
                         select new                         {
                             Title = g.Key.title,
                             Module = g.Key.module,
                             sum_num = g.Sum(r => Convert.ToInt32(r.Field<string>("Num")))
                         };遇到空值時:

sum_num = g.Sum(r => Convert.ToInt32(r.Field<Double?>("Num").GetValueOrDefault(0))) 
sum_num = g.Sum(r => Convert.ToInt32(r.Field<Double?>("Num").GetValueOrDefault(0))) 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.