LINQ 对数据进行两次分组
对于这个可怕的标题表示歉意,我不太确定如何表达我的问题。
我有一个看起来像这样的对象:
CustAcct cust = new CustAcct();
cust.Name = "John Doe";
cust.Account = "ABC123";
cust.OrderTotal = 123.43
cust.OrderQty = 4;
cust.TransDate = "12/26/2010 13:00"
请不要花太多时间批评下一部分,因为这确实不涉及购物车/客户的东西,但想法是相同的,我只是想使用每个人都在使用的东西相当熟悉。
一个账户可以有多个客户,一个客户也可以有多个账户。
所以你有:
List<CustAcct> custList = new List<CustAcct>();
custList.Add("John Doe", "ABC123", 123.43, 4, "12/26/2010 13:00");
custList.Add("John Doe", "ABC123", 32.12, 2, "12/27/2010 10:00");
custList.Add("John Doe", "ABC321", 43.34, 1, "12/28/2010 15:00");
custList.Add("John Doe", "ABC321", 54.60, 3, "12/28/2010 16:00");
custList.Add("Jane Zoe", "ABC123", 46.45, 2, "12/28/2010 17:00");
custList.Add("Jane Zoe", "ABC123", 32.65, 1, "12/29/2010 12:00");
custList.Add("Jane Zoe", "ABC321", 67.65, 3, "12/29/2010 23:00");
custList.Add("Jane Zoe", "ABC321", 75.34, 4, "12/30/2010 08:00");
我想做的是获取每个帐户和客户的所有 OrderTotal 和 OrderQty 的总和,这样我的输出将如下所示:
Account Customer OrderTotal OrderQty
ABC123 John Doe 155.55 6
ABC321 John Doe 97.94 4
ABC123 Jane Zoe 79.10 3
ABC321 Jane Zoe 142.99 7
我已经浏览了我的 LINQ to Objects 书和 101 个 LINQ 示例,但无法计算知道如何得到这个。谢谢。
Apologies for the terrible title, I wasn't quite sure how to phrase my problem.
I have an object that looks like:
CustAcct cust = new CustAcct();
cust.Name = "John Doe";
cust.Account = "ABC123";
cust.OrderTotal = 123.43
cust.OrderQty = 4;
cust.TransDate = "12/26/2010 13:00"
Please don't spend too much time criticizing the next part because this really doesn't deal with a shopping cart/Customer stuff but the idea is the same, I just wanted to use something that everyone is pretty familiar with.
An account can have more than one customer, and a customer can have more than one account.
So you have:
List<CustAcct> custList = new List<CustAcct>();
custList.Add("John Doe", "ABC123", 123.43, 4, "12/26/2010 13:00");
custList.Add("John Doe", "ABC123", 32.12, 2, "12/27/2010 10:00");
custList.Add("John Doe", "ABC321", 43.34, 1, "12/28/2010 15:00");
custList.Add("John Doe", "ABC321", 54.60, 3, "12/28/2010 16:00");
custList.Add("Jane Zoe", "ABC123", 46.45, 2, "12/28/2010 17:00");
custList.Add("Jane Zoe", "ABC123", 32.65, 1, "12/29/2010 12:00");
custList.Add("Jane Zoe", "ABC321", 67.65, 3, "12/29/2010 23:00");
custList.Add("Jane Zoe", "ABC321", 75.34, 4, "12/30/2010 08:00");
What I would like to do is get the sum of all OrderTotal and OrderQty for each Account and Customer so my output will look like:
Account Customer OrderTotal OrderQty
ABC123 John Doe 155.55 6
ABC321 John Doe 97.94 4
ABC123 Jane Zoe 79.10 3
ABC321 Jane Zoe 142.99 7
I've been through my LINQ to Objects book and 101 LINQ Samples and can't figure out how to go about getting this. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以像这样进行分组和求和:
查看实际操作。
You can group and sum like this:
See it in action.