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
you can make like this exampe: