我有3个表,试图通过左连接将它们组合在一起。在我的应用程序中,所有LINQ表达式都是查询链格式(lambda表达式)。
我正在尝试弄清楚如何使用LINQ完成此查询;
这是我的SQL查询;
select i.INVOICE_NUMBER, ic.CustomerName, o.BusinessUnit
from Invoices_Daily as i
left join intercompany as ic on i.customer_number = ic.customernumber
left join ordertypes as o on i.LINE_ORDERTYPE = o.OrderType
我已尝试如下组合GroupJoin
;
var commissions = data
.GroupJoin(genericNameList,
d => d.CUSTOMER_NUMBER,
g => g.CustomerId,
(d, g) => new { d, generic = g.FirstOrDefault() })
.GroupJoin(intercompanies,
dd => dd.d.CUSTOMER_NAME,
i => i.CustomerId,
(d, i) => new { data = d.d, intercompanies = i.FirstOrDefault() })
.Select(_ => new MainGridViewModel
{
INVOICE_DATE = _.d.INVOICE_DATE,
EndCustomer = _.generic == null ? _.d.CUSTOMER_NAME : _.generic.EndCustomer ?? _.d.CUSTOMER_NAME,
LINE_ORDERTYPE = _.i.OrderType
})
.ToList();
但是我遇到下面的语法错误;
错误CS0411无法从用法中推断出方法'Enumerable.GroupJoin(IEnumerable,IEnumerable,Func,Func,Func,TResult>)'的类型参数。尝试显式指定类型参数。
答案 0 :(得分:2)
我根据请求编写了一个测试示例,显示了正确的语法,这应该都可以正常工作。
public class Data
{
public string CUSTOMER_NUMBER { get; set; }
public string CUSTOMER_NAME { get; set; }
}
public class NameList
{
public string CustomerId { get; set; }
}
public class InterCompanies
{
public string CustomerId { get; set; }
}
public class Test
{
public void TMP()
{
var data = new List<Data>();
var genericNameList = new List<NameList>();
var intercompanies = new List<InterCompanies>();
var commissions = data
.GroupJoin(genericNameList,
d => d.CUSTOMER_NUMBER,
g => g.CustomerId,
(d, g) => new { d, generic = g.FirstOrDefault() })
.GroupJoin(intercompanies,
dd => dd.d.CUSTOMER_NAME,
i => i.CustomerId,
(d, i) => new { data = d.d, intercompanies = i.FirstOrDefault() })
.ToList();
}
}
我最初的想法是您CUSTOMER_NUMBER和CUSTOMER_NAME是错误的,因为为什么您在同一类中有2个字段具有相同的值。但是,既然您向我保证它们是正确的,也许您正在交换联接中数据上预期的字段顺序,因此很难看到数据模型。
也许您的第二次加入应如下所示:
.GroupJoin(intercompanies,
dd => dd.d.CUSTOMER_NUMBER,
i => i.CUSTOMER_NAME,