[英]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.