问题描述
对于这个可怕的标题抱歉,我不太确定如何解释我的问题。我有一个对象,如下所示:
CustAcct cust =新的CustAcct();
cust.Name =John Doe;
cust.Account =ABC123;
cust.OrderTotal = 123.43
cust.OrderQty = 4;
cust.TransDate =12/26/2010 13:00
t花太多时间批评下一部分,因为这实际上并没有处理购物车/客户的东西,但这个想法是一样的,我只是想用一些人人都很熟悉的东西。
一个账户可以有多个客户,而一个客户可以有多个账户。
所以你有:
列表< 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,2010年12月28日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);
我想要做的是获得每个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我已经通过我的LINQ to Objects书籍和101 LINQ Samples,并且无法弄清楚如何去做到这一点。谢谢。
您可以像这样进行分组和总结:
从custList中的ca获得
通过新的{ca.Name,ca.Account}转换成g
选择新的{
g.Key.Account,
g.Key.Name,
OrderTotal = g.Sum(o => o.OrderTotal),
OrderQty = g.Sum(o => o.OrderQty)
};
。
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.
You can group and sum like this:
from ca in custList group ca by new { ca.Name, ca.Account } into g select new { g.Key.Account, g.Key.Name, OrderTotal = g.Sum(o => o.OrderTotal), OrderQty = g.Sum(o => o.OrderQty) };
这篇关于LINQ将数据分组两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!