使用Lambda语法的C#LINQ Multiple GroupJoin [英] C# LINQ Multiple GroupJoin using Lambda Syntax

查看:50
本文介绍了使用Lambda语法的C#LINQ Multiple GroupJoin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个表,试图通过左连接将它们组合在一起.在我的应用程序中,所有LINQ表达式都是查询链格式(lambda表达式).

I have 3 tables that I am trying to combine them together using left joins. In my application, all LINQ expressions are in query-chain format (lambda expressions).

我试图弄清楚如何使用LINQ完成此查询;

I am trying to figure out how to accomplish this query using LINQ;

这是我的SQL查询;

This is my SQL query;

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

但是我遇到下面的语法错误;

However I am having a syntax error that is written below;

错误CS0411无法从用法中推断出方法'Enumerable.GroupJoin(IEnumerable,IEnumerable,Func,Func,Func,TResult>)'的类型参数.尝试显式指定类型参数.

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.

推荐答案

我根据请求编写了一个测试示例,其中显示了正确的语法,所有这些都应正常工作.

I wrote a test sample as per request showing the proper syntax, this should all be working.

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个字段具有相同的值.但是,既然您向我保证它们是正确的,也许您正在交换联接中数据上预期的字段顺序,因此很难看到数据模型.

My Original Thought was that you CUSTOMER_NUMBER, and CUSTOMER_NAME is wrong because why do you have 2 fields in the same class with the same value. But since you assured me they're correct, Maybe you're swapping the order of the fields expected on the data in the join, It's hard to tell without seeing your data models.

也许您的第二次加入应如下所示:

Perhaps your second join should look like so:

.GroupJoin(intercompanies,
            dd => dd.d.CUSTOMER_NUMBER,
            i => i.CUSTOMER_NAME,

这篇关于使用Lambda语法的C#LINQ Multiple GroupJoin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆