gpt4 book ai didi

C# LINQ Multiple GroupJoin 使用 Lambda 语法

转载 作者:行者123 更新时间:2023-11-30 23:00:20 26 4
gpt4 key购买 nike

我有 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();

但是我有一个语法错误,写在下面;

Error CS0411 The type arguments for method 'Enumerable.GroupJoin(IEnumerable, IEnumerable, Func, Func, Func, TResult>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

最佳答案

我根据请求编写了一个测试示例,显示了正确的语法,这应该一切正常。

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,

关于C# LINQ Multiple GroupJoin 使用 Lambda 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51617013/

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