Linq to Entity 左外连接
我有一个包含发票、AffiliateCommissions 和 AffiliateCommissionPayments 的实体模型。
发票到 AffiliateCommission 是一对多,AffiliateCommission 到 AffiliateCommissionPayment 也是一对多
我正在尝试进行查询,该查询将返回所有有佣金但不一定有相关佣金付款的发票。我想显示带有佣金的发票,无论它们是否有佣金付款。
查询看起来像这样:
using (var context = new MyEntitities())
{
var invoices = from i in context.Invoices
from ac in i.AffiliateCommissions
join acp in context.AffiliateCommissionPayments on ac.affiliateCommissionID equals acp.AffiliateCommission.affiliateCommissionID
where ac.Affiliate.affiliateID == affiliateID
select new
{
companyName = i.User.companyName,
userName = i.User.fullName,
email = i.User.emailAddress,
invoiceEndDate = i.invoicedUntilDate,
invoiceNumber = i.invoiceNumber,
invoiceAmount = i.netAmount,
commissionAmount = ac.amount,
datePaid = acp.paymentDate,
checkNumber = acp.checkNumber
};
return invoices.ToList();
}
上面的查询仅返回带有 AffiliateCommissionPayment 的项目。
I have an Entity model with Invoices, AffiliateCommissions and AffiliateCommissionPayments.
Invoice to AffiliateCommission is a one to many, AffiliateCommission to AffiliateCommissionPayment is also a one to many
I am trying to make a query that will return All Invoices that HAVE a commission but not necessarily have a related commissionPayment. I want to show the invoices with commissions whether they have a commission payment or not.
Query looks something like:
using (var context = new MyEntitities())
{
var invoices = from i in context.Invoices
from ac in i.AffiliateCommissions
join acp in context.AffiliateCommissionPayments on ac.affiliateCommissionID equals acp.AffiliateCommission.affiliateCommissionID
where ac.Affiliate.affiliateID == affiliateID
select new
{
companyName = i.User.companyName,
userName = i.User.fullName,
email = i.User.emailAddress,
invoiceEndDate = i.invoicedUntilDate,
invoiceNumber = i.invoiceNumber,
invoiceAmount = i.netAmount,
commissionAmount = ac.amount,
datePaid = acp.paymentDate,
checkNumber = acp.checkNumber
};
return invoices.ToList();
}
This query above only returns items with an AffiliateCommissionPayment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定 EF 是否支持此功能(我也不确定您使用的是 EF2 还是 EF4),但这是 Linq2Sql 中的解决方案,因此可能值得尝试:
这里的主要更改是
into acpgjoin
之后的 code> 和DefaultIfEmpty
行。I'm not sure if EF supports this (nor am I sure if you are using EF2 or EF4), but this is the solution in Linq2Sql so it might be worth trying:
The main change here is the
into acpg
after yourjoin
, and theDefaultIfEmpty
line.在 LINQ 中使用
join
几乎总是一个错误SQL 和 LINQ to Entities。猜测从
AffiliateCommission
到AffiliateCommissionPayment
的关联称为Payment
,您可以这样做:LINQ to SQL 和 LINQ to Entities 都将合并 null。强制转换是必要的,因为推断的类型将基于
AffiliateCommissionPayment. paymentDate
的类型,该类型可能不可为 null。如果是的话,你就不需要演员了。It's almost always a mistake to use
join
in LINQ to SQL and LINQ to Entities.Guessing that the association from
AffiliateCommission
toAffiliateCommissionPayment
is calledPayment
, you can just do:LINQ to SQL and LINQ to Entities will both coalesce nulls. The casts are necessary because the inferred type will be based on the type of
AffiliateCommissionPayment.paymentDate
, which might not be nullable. If it is, you don't need the cast.