问题
I have got 3 table.
For instance Client, Company and Address.
Client has got ref to Company.
Company has got 2 nullable refs to Address (Billing and Shipping), so Address may not exist in some case.
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.
回答1:
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
});
回答2:
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
};
来源:https://stackoverflow.com/questions/13341858/linq-join-query-with-nullable-ref-between-table