c# - LINQ 连接查询(表之间可以为空的引用)

标签 c# linq entity-framework nullable

我有3张 table 。

例如客户公司地址

  • 客户已引用公司。

  • 公司有 2 个可空的地址引用(账单和送货),因此在某些情况下地址可能不存在。

我需要进行连接查询,但是当 Company.BillingAddressCompany.ShippingAddress 等于 null 时,我无法得到所有信息数据)。

我试过了(但查询错误):

var res = (from client in context.Clients
    join clientCompany in context.Companies 
    on client.ClientCompanyId equals clientCompany.Id

    into clientCompanyJoin

    from company in clientCompanyJoin
    join addressBilling in context.Addresses
    on company.BillingAddressId equals addressBilling.Id

    join addressShipping in context.Addresses
    on company.ShippingAddressId equals addressShipping.Id

    select new
    {
        Client = client,
        Company = company,
        BillingAddress = ???????
        ShippingAddress = ???????
    }
);

你能帮我做一个连接查询或者解释一下怎么做吗?

谢谢。

最佳答案

试试这段代码片段:

var res = (from client in context.Clients
            join clientCompany in context.Companies 
            on client.ClientCompanyId equals clientCompany.Id
            into clientCompanyJoin
            from company in clientCompanyJoin
            join addressBilling in context.Addresses
            on company.BillingAddressId equals addressBilling.Id
            where !String.IsNullOrEmpty(addressBilling.Address)
            join addressShipping in context.Addresses
            on company.ShippingAddressId equals addressShipping.Id
            where !String.IsNullOrEmpty(addressShipping.Address)
            select new
            {
                Client = client,
                Company = company,
                BillingAddress = addressBilling.Address,
                ShippingAddress = addressShipping.Address
            });

已添加:根据您的评论,这是您需要的代码片段。您现在可以拥有您的客户公司数据,即使ShippingAddressIdBillingAddressId 等于null 就像 Left JoinSQL 中所做的那样。

var res = (from client in context.Clients
            join company in context.Companies 
            on client.ClientCompanyId equals company.Id
            join addressBilling in context.Addresses
            on company.BillingAddressId equals addressBilling.Id 
            into addrBillingGroup
            from gAddrBilling in addrBillingGroup.DefaultIfEmpty() // left join
            join addressShipping in context.Addresses
            on company.ShippingAddressId equals addressShipping.Id 
            into addrShippingGroup
            from gAddrShipping in addrShippingGroup.DefaultIfEmpty() // left join
            select new
            {
                Client = client,
                Company = company,
                BillingAddress = 
                    gAddrBilling == null ? null : gAddrBilling.Address,
                ShippingAddress = 
                    gAddrShipping == null ? null : gAddrShipping.Address
            });

关于c# - LINQ 连接查询(表之间可以为空的引用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13341858/

相关文章:

c# - 同时获取多个线程同步锁

c# - 开源项目管理软件

c# - FirstOrDefault 是否在 LINQ 中延迟?

c# - Linq 的 Skip and Take 是否针对数组进行了优化? [4.0版]

c# - Entity Framework 正在尝试在数据库上查找已删除的外键

c# - Entity Framework 6.1 和使用新关键字隐藏成员

c# - 无法将对象转换为字符串 C#?

c# - 删除文件锁

c# - 锯齿状到多维数组

c# - 为什么 Entity Framework 能够检索实体但不能保存/更新/删除它们?