Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于特定属性的相同类型的堆栈列表元素_C#_Linq_C# 4.0 - Fatal编程技术网

C# 基于特定属性的相同类型的堆栈列表元素

C# 基于特定属性的相同类型的堆栈列表元素,c#,linq,c#-4.0,C#,Linq,C# 4.0,我尝试(使用LINQ)堆叠相同类型的元素列表,然后增加一个匿名属性 ElementA ElementA ElementB ElementB ElementB ElementC 成为一个匿名对象,公开元素和数量 ElementA (Property Qty = 2) ElementB (Property Qty = 3) ElementC (Property Qty = 1) 使用LINQ有什么优雅的方法可以做到这一点吗?不确定这是否有效,但肯定是可能的。类似于 var totals = fr

我尝试(使用LINQ)堆叠相同类型的元素列表,然后增加一个匿名属性

ElementA
ElementA
ElementB
ElementB
ElementB
ElementC
成为一个匿名对象,公开元素和数量

ElementA (Property Qty = 2)
ElementB (Property Qty = 3)
ElementC (Property Qty = 1)

使用LINQ有什么优雅的方法可以做到这一点吗?

不确定这是否有效,但肯定是可能的。类似于

var totals = from e in <element list>
             group e by e.<property> into g
             select new {
                Property = g.Key,
                Count = g.Count()
             }
var总计=从e到in
e组为e组。进入g
选择新的{
属性=g.键,
Count=g.Count()
}
我会给你们一个财产清单


在本例中,
g
属于类型,它继承自
IEnumerable
,因此它可以执行
IEnumerable
可以执行的任何操作。

是否有方法基于GroupBy返回第二个元素列表(匿名)?你有使用GroupBy的例子吗?还有更多的例子。太棒了!我不知道我们可以用Count来做这个。这就是我们所缺少的。我用Sum做了很多组合。。但我什么也没得到。