3

我有 3 张桌子。

例如客户公司地址

  • 客户已获得公司的参考。

  • 公司有 2 个可空的地址引用(账单和运输),所以地址在某些情况下可能不存在。

我需要进行连接查询,但如果 whenCompany.BillingAddressCompany.ShippingAddressequals 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 = ???????
    }
);

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

谢谢。

4

2 回答 2

6

试试这段代码片段:

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与.nullLeft 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
            });
于 2012-11-12T10:33:36.793 回答
2

我猜你想做一个外部连接。以下是如何在“Northwind”数据库中为客户和订单执行此操作的示例:

var ctx = new NorthwindDataContext();
var customers = from c in ctx.Customers
    join o in ctx.Orders
    on c.CustomerID equals o.CustomerID into inJoin
    from outJoin in inJoin.DefaultIfEmpty()
    orderby c.CustomerID, outJoin.OrderID
    select new
    {
        c.CustomerID,
        c.CompanyName,
        OrderID = (int?)outJoin.OrderID,
        OrderDate = (DateTime?)outJoin.OrderDate
    };
于 2012-11-12T10:26:05.307 回答