2013-01-16 61 views
3

我有一張這樣的桌子。linq count by 2 conditions

Age Group 

20 G1 
21 G2 
20 G2 
21 G1 
22 G3 
21 G1 
20 G2 
21 G2 
22 G3 
20 G1 
22 G3 

我想要的結果是

Age Grp Count 

20 G1 2 
20 G2 2 
21 G1 2 
22 G2 2 
22 G3 3 

我不能弄清楚如何通過2組來計算

我只有1組

var thirdResultQuery=from row in secondResultQuery 
         group row by new {row.Grp } into g 
         select new 
         { 

          g.Key.Grp, 
          ageCount = g.Count() 
         }; 

任何想法?

回答

3

GROUP

var thirdResultQuery = from row in secondResultQuery 
          group row by new {row.Grp, row.Age} into g 
          select new 
          { 
           g.Key.Age, 
           g.Key.Grp, 
           ageCount = g.Count() 
          }; 
+0

哦,是啊..這是我以前的查詢添加Age。爲什麼我認爲這不起作用?謝謝你.. – william

+0

@william你以前的查詢是什麼? –

+0

@ JW,你能回答嗎? http://stackoverflow.com/questions/14351780/combining-two-datatable-and-getting-the-amount-from-second-table-by-using-linq – william