与和
问题描述:
分组数据我有以下列表:与和
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
答
目前还不清楚到底是什么你实际上分组通过 - 它是既重和停产状态?如果是这样,你可以这样做:
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);
}
答
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
答
“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()});
如何展示你的企图? – 2009-02-12 09:46:27