LINQ 对数据进行两次分组

发布于 2024-12-11 01:03:41 字数 1352 浏览 0 评论 0原文

对于这个可怕的标题表示歉意,我不太确定如何表达我的问题。

我有一个看起来像这样的对象:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉态萌生 2024-12-18 01:03:41

您可以像这样进行分组和求和:

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)
};

查看实际操作

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)
};

See it in action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文