C# LINQ连接查询(表之间的引用可为null)
我有三张桌子 例如客户、公司和地址C# LINQ连接查询(表之间的引用可为null),c#,linq,entity-framework,nullable,C#,Linq,Entity Framework,Nullable,我有三张桌子 例如客户、公司和地址 客户已经得到了公司的推荐 该公司有2个可为空的参考地址(账单和发货),所以在某些情况下地址可能不存在 我需要进行连接查询,但如果Company.BillingAddress或Company.ShippingAddress等于null,我无法获取所有数据) 我试过(但它是错误的): 你能帮我做一个join查询或者解释一下怎么做吗 谢谢。我想你想做一个外部连接。以下是如何在客户和订单的“Northwind”数据库中执行此操作的示例: var ctx = new
- 客户已经得到了公司的推荐
- 该公司有2个可为空的参考地址(账单和发货),所以在某些情况下地址可能不存在
Company.BillingAddress
或Company.ShippingAddress
等于null
,我无法获取所有数据)
我试过(但它是错误的):
你能帮我做一个join查询或者解释一下怎么做吗
谢谢。我想你想做一个外部连接。以下是如何在客户和订单的“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
};
请尝试以下代码片段:
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
});
添加:根据您的评论,这里是您需要的代码片段。即使ShippingAddressId
或BillingAddressId
equalnull
与在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
});
加入什么?返回类型的结构应该是什么?你能发布一些代码或伪代码吗?它不起作用。我不明白一些事情(例如,where!String.IsNullOrEmpty(addressBilling.Address))和addressBilling不包含地址字段。我认为这些地方的问题“on company.BillingAddressId等于addressBilling.Id”因为在某些情况下,BillingAddressId和ShippingAddressId等于NULL是否要筛选BillingAddressId和ShippingAddressId等于NULL的记录?其次,如果BillingAddressId和ShippingAddressId是整数类型,那么它怎么可以为NULL?是的,如果ShippingAddressId或BillingAddressId等于NULL,我仍然需要客户和公司数据,但没有地址(在sql中,左连接结果应该为NULL)。在我的模型中,ShippingAddressId和BillingAddressId是可以为NULL的int字段。
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
});