C#列表<> GROUPBY 2价值观 [英] C# List<> GroupBy 2 Values

查看:126
本文介绍了C#列表<> GROUPBY 2价值观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Framework 3.5的C#。我期待迅速组泛型列表<>由两个属性。在这个例子中的缘故可以说我有一个订单类型与客户编号,产品编号和ProductCount的属性的列表。我怎么会得到由客户编号分组ProductCounts之和使用lambda表达式的ProductID?


解决方案

  VAR金额= Orders.GroupBy(X =>新建{x.Cu​​stomerID,x.ProductID})
。选择(组=> group.Sum(X => x.ProductCount));


I'm using C# on Framework 3.5. I'm looking to quickly group a Generic List<> by two properties. For the sake of this example lets say I have a List of an Order type with properties of CustomerId, ProductId, and ProductCount. How would I get the sum of ProductCounts grouped by CustomerId and ProductId using a lambda expression?

解决方案

var sums = Orders.GroupBy(x => new { x.CustomerID, x.ProductID })
                 .Select(group => group.Sum(x => x.ProductCount));

这篇关于C#列表&LT;&GT; GROUPBY 2价值观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆