【发布时间】:2011-10-21 17:45:57
【问题描述】:
为糟糕的标题道歉,我不太确定如何表达我的问题。
我有一个看起来像这样的对象:
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 示例,但不知道如何获得这些。谢谢。
【问题讨论】:
-
你是使用方法语法还是查询语法?
-
我正在使用查询语法。来自 CustList 中的 c...
-
您想按帐户/客户组合进行分组吗?或者只是按客户分组,然后按帐户?细微的差别,但会影响我的回答...
-
按客户/客户组合