gpt4 book ai didi

linq - 如何在 LINQ 中对多个表进行分组

转载 作者:行者123 更新时间:2023-12-04 07:05:02 25 4
gpt4 key购买 nike

所以,我理解 group by 的工作原理基本上是这样的:

var query = from c in context.Contacts
join o in context.Orders on c.Id on o.CustomerId
group c by o.Id
select new { g.Key, g.Sum(c => c.Whatever) };

这样的分组只允许我访问 c 的内容。但是如果我想要来自表 c 和 o 的数据怎么办?
var query = from c in context.Contacts
join o in context.Orders on c.Id on o.CustomerId
//insert answer here
select new { g.Key, g.Sum(c => c.Whatever), g.Sum(o => o.Whatever) };

这甚至可能吗?

最佳答案

var query = from c in context.Contacts
join o in context.Orders on c.Id equals o.CustomerId
select new
{
Contact = c,
Order = o
} into ContactAndOrder
group ContactAndOrder by ContactAndOrder.Order.Id into g
select new
{
g.Key,
ContactWhatever = g.Sum(co => co.Contact.Whatever),
OrderWhatever = g.Sum(co => co.Order.Whatever)
};

关于linq - 如何在 LINQ 中对多个表进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1241405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com