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

214 views Asked by At

I have the following 5 models. I want to join and group them with LINQ in my ASP Core API Application. I want to join all my JournalVoucher,PurchaseInvoice and SaleInvoice to calculate Item Input And Output And Remain quantity of an item. please help me to write the LINQ query with best practise,

      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.

sale invoice quantity = QuantityOut

Purchase Invoice Quantity= QuantityIn

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

There are 1 answers

0
Michael Rodrigues On

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
}