Linq to Entity 左外连接

发布于 2024-08-25 16:48:23 字数 1026 浏览 8 评论 0原文

我有一个包含发票、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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

執念 2024-09-01 16:48:23

我不确定 EF 是否支持此功能(我也不确定您使用的是 EF2 还是 EF4),但这是 Linq2Sql 中的解决方案,因此可能值得尝试:

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 into acp_join
   from acp_join_default in acpg.DefaultIfEmpty()
   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(); 
} 

这里的主要更改是 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:

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 into acp_join
   from acp_join_default in acpg.DefaultIfEmpty()
   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(); 
} 

The main change here is the into acpg after your join, and the DefaultIfEmpty line.

不可一世的女人 2024-09-01 16:48:23

在 LINQ 中使用 join 几乎总是一个错误SQL 和 LINQ to Entities

猜测从 AffiliateCommissionAffiliateCommissionPayment 的关联称为 Payment,您可以这样做:

using (var context = new MyEntitities())
{
   var invoices = from i in context.Invoices
   from ac in i.AffiliateCommissions
   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 = (DateTime?) ac.Payment.paymentDate,
      checkNumber = (int?) ac.Payment.checkNumber
   };
   return invoices.ToList();
}

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 to AffiliateCommissionPayment is called Payment, you can just do:

using (var context = new MyEntitities())
{
   var invoices = from i in context.Invoices
   from ac in i.AffiliateCommissions
   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 = (DateTime?) ac.Payment.paymentDate,
      checkNumber = (int?) ac.Payment.checkNumber
   };
   return invoices.ToList();
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文