如何在 Entity Framework Linq 中左加入 2 个子选择?
How to Left join with 2 subselects in Entity Framework Linq?
如何使用 Entity Framework 在 c# 中编写此查询?
Select Id, Name, DeliveryAdress, InvoiceAdress from Company
left join (Select Address.CompanyId as CompanyId, Street + ' ' + ZipCode + ' ' + City as DeliveryAdress from Address
join AddressType on AddressType.AddressTypeId = Address.AddressTypeId
where AddressType.Name = 'Delivery') sub1 on sub1.CompanyId = Company.Id
left join (Select Address.CompanyId as CompanyId, Street + ' ' + ZipCode + ' ' + City as InvoiceAdress from Address
join AddressType on AddressType.AddressTypeId = Address.AddressTypeId
where AddressType.Name = 'Invoice') sub2 on sub2.CompanyId = Company.Id
事实上我有 3 个表:公司、地址和地址类型,在这个查询中我必须 select 来自公司的 ID 和名称以及 2 个地址 select 按类型名编辑...
首先通过连接 Address 和 AddressTypes 获取 DeliveryAddresses 列表
var deliveryaddresses=
from addr in db.Addresses join (addrtype in
db.AddressTypes.Where(at=>at.Name=="Delivery")) on addr.AddressTypeId equals
addrtype.AddressTypeId select new {CompanyId=addr.CompanyId,
DeliveryAddress= addr.Street + " " + ZipCode + " " + City}
接下来通过连接地址和地址类型获取发票地址列表
var invoiceaddresses=
from addr in db.Addresses join (addrtype in
db.AddressTypes.Where(at=>at.Name=="Invoice")) on addr.AddressTypeId equals
addrtype.AddressTypeId select new {CompanyId=addr.CompanyId,
InvoiceAddress= addr.Street + " " + ZipCode + " " + City}
然后对 DeliveryAddresses 和 invoiceAddresses 执行 Left Outer Join
var deliveryandinvoiceaddresses=from da in deliveryaddresses join ia in
invoiceaddresses on da.CompanyId equals ia.companyId into addrgroup from ag
in addrgroup.DefaultIfEmpty() select new
{CompanyId=da.CompanyId,DeliveryAddress=da.DeliveryAddress,InvoiceAddress=
(ag==null?string.Empty:ag.InvoiceAddress)}
最后对 Company 执行 Left Outer Join 以及交货地址和发票地址的外部连接结果
var completelist=from c in db.companys join d in deliveryandinvoiceaddresses
on c.Id equals d.CompanyId into compgroup from cg in
companygroup.DefaultIfEmpty() select new {CompanyId=c.Id,
Name=c.Name,DeliveryAddress=(cg==null?string.Empty:cg.DeliveryAddress),
InvoiceAddress=(cg==null?string.Empty:cg.InvoiceAddress)}
希望它能解决问题。
如何使用 Entity Framework 在 c# 中编写此查询?
Select Id, Name, DeliveryAdress, InvoiceAdress from Company
left join (Select Address.CompanyId as CompanyId, Street + ' ' + ZipCode + ' ' + City as DeliveryAdress from Address
join AddressType on AddressType.AddressTypeId = Address.AddressTypeId
where AddressType.Name = 'Delivery') sub1 on sub1.CompanyId = Company.Id
left join (Select Address.CompanyId as CompanyId, Street + ' ' + ZipCode + ' ' + City as InvoiceAdress from Address
join AddressType on AddressType.AddressTypeId = Address.AddressTypeId
where AddressType.Name = 'Invoice') sub2 on sub2.CompanyId = Company.Id
事实上我有 3 个表:公司、地址和地址类型,在这个查询中我必须 select 来自公司的 ID 和名称以及 2 个地址 select 按类型名编辑...
首先通过连接 Address 和 AddressTypes 获取 DeliveryAddresses 列表
var deliveryaddresses=
from addr in db.Addresses join (addrtype in
db.AddressTypes.Where(at=>at.Name=="Delivery")) on addr.AddressTypeId equals
addrtype.AddressTypeId select new {CompanyId=addr.CompanyId,
DeliveryAddress= addr.Street + " " + ZipCode + " " + City}
接下来通过连接地址和地址类型获取发票地址列表
var invoiceaddresses=
from addr in db.Addresses join (addrtype in
db.AddressTypes.Where(at=>at.Name=="Invoice")) on addr.AddressTypeId equals
addrtype.AddressTypeId select new {CompanyId=addr.CompanyId,
InvoiceAddress= addr.Street + " " + ZipCode + " " + City}
然后对 DeliveryAddresses 和 invoiceAddresses 执行 Left Outer Join
var deliveryandinvoiceaddresses=from da in deliveryaddresses join ia in
invoiceaddresses on da.CompanyId equals ia.companyId into addrgroup from ag
in addrgroup.DefaultIfEmpty() select new
{CompanyId=da.CompanyId,DeliveryAddress=da.DeliveryAddress,InvoiceAddress=
(ag==null?string.Empty:ag.InvoiceAddress)}
最后对 Company 执行 Left Outer Join 以及交货地址和发票地址的外部连接结果
var completelist=from c in db.companys join d in deliveryandinvoiceaddresses
on c.Id equals d.CompanyId into compgroup from cg in
companygroup.DefaultIfEmpty() select new {CompanyId=c.Id,
Name=c.Name,DeliveryAddress=(cg==null?string.Empty:cg.DeliveryAddress),
InvoiceAddress=(cg==null?string.Empty:cg.InvoiceAddress)}
希望它能解决问题。