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

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

问题描述

我在 Framework 3.5 上使用 C#.我希望通过两个属性快速分组一个通用列表<>.就本示例而言,假设我有一个 Order 类型的 List,其中包含 CustomerId、ProductId 和 ProductCount 的属性.如何使用 lambda 表达式获得按 CustomerId 和 ProductId 分组的 ProductCounts 的总和?

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天全站免登陆