简体   繁体   English

我如何使用 Linq 表达式在 EF Core 3.1 中的多个表上加入

[英]How can i use Linq expression for Join on multiple table in EF Core 3.1

I have the following 5 models.我有以下5个模型。 I want to join and group them with LINQ in my ASP Core API Application.我想在我的 ASP Core API 应用程序中加入 LINQ 并将它们分组。 I want to join all my JournalVoucher,PurchaseInvoice and SaleInvoice to calculate Item Input And Output And Remain quantity of an item.我想加入我所有的 JournalVoucher、PurchaseInvoice 和 SaleInvoice 来计算项目输入和 Output 和项目的剩余数量。 please help me to write the LINQ query with best practise,请帮助我以最佳实践编写 LINQ 查询,

      public partial class JournalVoucher : BaseEntity
      {
          //......
         public DateTime VoucherDate{ get; set; }
         public ICollection<Invoice> Invoices { get; set; }
         public ICollection<PurchaseInvoice> PurchaseInvoices { get; set; }            
      }         

Purchase Invoice Table采购发票表

               public partial class PurchaseInvoice : BaseEntity
               {
                   //...

                   public int Type{ get; set; }
                   public JournalVoucher JournalVoucher { get; set; }
                   public ICollection<PurchaseInvoiceLine> PurchaseInvoiceLines { get; set; }

               }

             public partial class PurchaseInvoiceLine : BaseEntity
             {
               //....
              public int ItemId { get; set; }
              public int UnitId { get; set; }
              public decimal Quantity { get; set; }
              public decimal Discount { get; set; }
              public decimal Tax { get; set; }
              public PurchaseInvoice PurchaseInvoice { get; set; } 
             }

Sale Invoice Table销售发票表

  public partial class SaleInvoice : BaseEntity
               {
                   //...
                   public int Type{ get; set; }
                   public JournalVoucher JournalVoucher { get; set; }
                   public ICollection<SaleInvoiceLine> SaleInvoiceLines { get; set; }

               }

             public partial class SaleInvoiceLine : BaseEntity
             {
               //....
              public int ItemId { get; set; }
              public int UnitId { get; set; }
              public decimal Quantity { get; set; }
              public decimal Discount { get; set; }
              public decimal Tax { get; set; }
              public SaleInvoice SaleInvoice { get; set; } 
             }

My ViewModel我的视图模型

   public class ItemUsedInPurchaseAndInvoiceViewModel
   {

    public int ItemId { get; set; }
    public int UnitId { get; set; }
    public decimal UnitPrice { get; set; }
    public decimal Discount { get; set; }
    public decimal Tax { get; set; }
    public decimal QuantityIn { get; set; }
    public decimal QuantityOut { get; set; }
    public DateTime VoucherDate{ get; set; }
    public int Type{ get; set; }
    public decimal TotalPriceIn => ((UnitPrice * QuantityIn - Discount) + Tax);
    public decimal TotalPriceOut => ((UnitPrice * QuantityOut - Discount) + Tax);
    public decimal TotalRemain => QuantityIn - QuantityOut;

}

Now I want the following result data by joining these five tables with group by.现在我想要通过将这五个表与 group by 连接来获得以下结果数据。

sale invoice quantity = QuantityOut销售发票数量 = QuantityOut

Purchase Invoice Quantity= QuantityIn采购发票数量= QuantityIn

ItemId  UnitId  UnitPrice Discount   Tax   Duty  Type  TotalPriceIn  QuantityIn QuantityOut
  10      1          10      2         1     0      1       19          2           1
 

you can make like this exampe:你可以做这样的例子:

    from c in Customers
join p in Purchases on c.ID equals p.CustomerID           // first join
join pi in PurchaseItems on p.ID equals pi.PurchaseID     // second join
select new
{
    c.Name, p.Description, pi.Detail
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 EF Core 6 中将 SQL 查询转换为具有各种条件的 Join on 的 Linq 表达式? - How can I convert a SQL query to a Linq expression for a Join on with various conditions in EF Core 6? 无法在 EF Core 3.1 中翻译 LINQ 表达式 - The LINQ expression could not be translated in EF Core 3.1 如何在带有Join的EF Where()子句中使用表达式树? - How can I use a Expression tree in an EF Where() clause with join? EF Core 3.1 - 无法翻译 LINQ 表达式(左连接与 group by) - EF Core 3.1 - The LINQ expression could not be translated (left joins with group by) Linq Expression EF核心 - Linq Expression EF core 在 EF Core 3.1 中加入群组 - Group join in EF Core 3.1 无法翻译 LINQ 表达式。 以可翻译的形式重写查询,或切换到客户端评估 EF Core 3.1 - The LINQ expression could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation EF Core 3.1 LINQ 加入 .NET 核心 3.1 - LINQ JOIN in .NET Core 3.1 如何直接填充 EF Core 为多对多关系生成的连接表? - How can I directly fill join table generated by EF Core for many-to-many relationship? 如何在EF Core中仅包含某些列的多个表中联接数据集? - How can I join datasets from multiple tables with only certain columns in EF Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM