与和

2009-02-12 76 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()});