與和

2009-02-12 59 views
0

分組數據我有以下列表:與和

mat.Add(new Material() { ID = 1, ProdName = "Cylinder", Weight=23, Discontinued='N' }); 
mat.Add(new Material() { ID = 2, ProdName = "Gas", Weight = 25, Discontinued='N' }); 
mat.Add(new Material() { ID = 3, ProdName = "Match", Weight = 23, Discontinued='N' }); 

我想要的結果:

2 Products have Weight 23 and Discontinued N 
1 Product have Weigth 25 Discontinued N 
+0

如何展示你的企圖? – 2009-02-12 09:46:27

回答

1

目前還不清楚到底是什麼你實際上分組通過 - 它是既重停產狀態?如果是這樣,你可以這樣做:

var query = mat.GroupBy(material => 
         new { material.Weight, material.Discontinued }); 

foreach (var result in query) 
{ 
    Console.WriteLine("{0} products have {1}", result.Count(), result.Key); 
} 
0

Got it!

.GroupBy(re => new { re.Weight, re.Discontinued }) 
      .Select(grp => new { CXXX = group.Key, Count = grp.Count() 
+1

如果你保持相同的屬性,如您的問題這將有助於。這與首先​​進行總結(按照您的標題)有什麼關係? – 2009-02-12 09:51:22

0

「just」group by Discontinued。

類似於: var result = mat.GroupBy(a => new {a.Weight,a.Discontinued})。選擇(b => new b.Key.Weight,b.Key.Discontinued,Count = b.Count()});