我有一个具有子集合的类。事情是这样的:LINQ嵌套集合
public class Item {
public string Type {get;set}
public Subitem[] {get;set;}
}
我知道,我可以单独和按类型统计是这样的:
var count = (from x in items
group x by x.Type into grouped
select new {
typename = grouped.Key,
count = grouped.Count()
}).ToDictionary<string, int>(x => x.typename, x => x.count);
这将返回是这样的:
{TYPE1,13} {2型,26}
等等。
但是我怎样才能通过Subitem来计算?
返回类似: {的SubItem1,15} {subitem2,46}
莫非你认罪告诉我们在你的例子中subitem1和subitem2的数字是什么?这些项目或子项目的计数? 是否要计算子项目列表(数组)中具有子项目1的项目数? – 2012-01-28 23:52:24
是的!这些都是重要的。在该示例中,在整个Items集合中有15个项目具有subitem1。 – programad 2012-01-29 00:11:12