Serhii Kyslyi
Serhii Kyslyi

Reputation: 1823

LINQ Join query (with nullable ref between table)

I have got 3 table.

For instance Client, Company and Address.

I need make join query, but in case when Company.BillingAddress or Company.ShippingAddress equals null, I don't get all data).

I tried it (but it's wrong query):

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 = ???????
    }
);

Could you please help me to make a join query or explain how to do it?

Thanks.

Upvotes: 3

Views: 9274

Answers (2)

Furqan Safdar
Furqan Safdar

Reputation: 16698

Try this piece of code snippet:

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

ADDED: As per your comments, here is the piece of code snippet you need. You can now have your Client and Company data even if ShippingAddressId or BillingAddressId equal null like what Left Join do in SQL.

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

Upvotes: 6

eugenesqr
eugenesqr

Reputation: 579

I guess you want to make an outer join. Here is an example how to do it on the "Northwind" database for customers and orders:

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
    };

Upvotes: 2

Related Questions