LINQ 多个联接与一个分组依据
本文关键字:一个 LINQ | 更新日期: 2023-09-27 18:35:02
在尝试了很多之后,我决定在这里发布一个问题。我的代码如下:
var query =
//Customers
from customer in context.Customers
//Engineer 1
join engineer1 in context.Employees on customer.PrimaryEngineer equals
engineer1.EmployeeId into eng1
from engineer1 in eng1.DefaultIfEmpty()
//Engineer Top
join engineerTop in context.Employees on customer.TopEngineer equals
topEngineer.EmployeeId into top
from engineerTop in top.DefaultIfEmpty()
然后,我想要的我按三个表中的属性分组:客户,员工(每个工程师都重复此操作(
如何按三个表中的属性进行分组?
下一个代码:
group new {customer, engineer1, engineerTop} by new {customer.Name .....}
由于 engineer1 和 engineerTop 来自同一上下文而不起作用?
我对此感到非常困惑。
谢谢大家的帮助
由于多个具有相同名称的属性,您可能需要为分组中的匿名类型属性分配一个名称。(是员工ID的两倍?
var query = (
//Customers
from customer in context.Customers
//Engineer 1
join engineer1 in context.Employees on customer.PrimaryEngineer equals
engineer1.EmployeeId into eng1
from engineer1 in eng1.DefaultIfEmpty()
//Engineer Top
join engineerTop in context.Employees on customer.TopEngineer equals
engineerTop.EmployeeId into top
from engineerTop in top.DefaultIfEmpty()
group new {customer, engineer1, engineerTop} by new {CustName = customer.Name, EmpId1 = engineer1.EmployeeId, EmpId2 = engineerTop.EmployeeId} into grp
select new
{
Name = grp.Key.CustName,
EmpId1 = grp.Key.EmpId1,
EmpId2 = grp.Key.EmpId2
});