在linq查询中连接多个表

时间:2017-11-24 18:59:18

标签: linq join left-join

我有四张相关的表格

1. Sales - {Sales_Id, Invoice_No, Invoice_Amount}
2. Sales_Details {Sale_Detail_Id, Sales_Id, Employee_Id,Commission}
3. Employee {Employee_Id, Employee_Name, Region_Id}
4. Region {Region_Id, Region_Name}

有两种员工一起进行一次销售的情况。我需要一个查询来获取 Sum(Invoice_Amount)和Region_Name by Region_Name

当我尝试运行以下查询时,如果由两名员工进行销售,我将获得双重记录。如何解决这个问题?

from sale in cntx.Sales
join dtls in cntx.Sales_Details on sale.Sales_Id equals dtls.Sales_Id
join emp in cntx.Employee on dtls.Employee_Id equals emp.Employee_Id
join region in cntx.Region on emp.Region_Id equals region.Region_Id
group new
{
 region.Region_Name,
 sale.Invoice_Amount,
} by new { region.Region_Name } into g
orderby (g.Key.Region_Name)
select new
{
 regionName = g.Key.Region_Name,
 sumofInvoice = g.Sum(x => x.Invoice_Amount),
 counter = g.Count()
};

0 个答案:

没有答案
相关问题