C# SQL到Linq,在SUM和多列group by与联接中遇到问题
我想将下面的查询转换为linq,以便在MVC中进行分页C# SQL到Linq,在SUM和多列group by与联接中遇到问题,c#,linq,entity-framework,join,group-by,C#,Linq,Entity Framework,Join,Group By,我想将下面的查询转换为linq,以便在MVC中进行分页 SELECT tblInvoiceMaster.InvoiceNo, tblCompanyMaster.CompanyId, tblCompanyMaster.CompanyName, tblInvoiceMaster.InvoiceType, tblInvoiceMaster.InvoiceId, Sum(tblInvoiceItem.Quantit
SELECT tblInvoiceMaster.InvoiceNo,
tblCompanyMaster.CompanyId,
tblCompanyMaster.CompanyName,
tblInvoiceMaster.InvoiceType,
tblInvoiceMaster.InvoiceId,
Sum(tblInvoiceItem.Quantity * tblProductMaster.Rate) AS TotalPrice,
CONVERT(VARCHAR(11), tblInvoiceMaster.InvoiceDate, 113) AS InvoiceDate
FROM tblCompanyMaster
INNER JOIN tblInvoiceMaster
ON tblCompanyMaster.CompanyId = tblInvoiceMaster.CompanyId
INNER JOIN tblInvoiceItem
ON tblInvoiceMaster.InvoiceId = tblInvoiceItem.InvoiceId
INNER JOIN tblProductMaster
ON tblProductMaster.ProductId = tblInvoiceItem.ProductId
GROUP BY tblInvoiceMaster.InvoiceNo,
tblCompanyMaster.CompanyId,
tblCompanyMaster.CompanyName,
tblInvoiceMaster.InvoiceDate,
tblInvoiceMaster.InvoiceId,
tblInvoiceMaster.InvoiceType
等效Linq查询:
(from company in DbContext.tblCompanyMasters
join invoice in DbContext.tblInvoiceMasters
on company.CompanyId equals invoice.CompanyId
join item in DbContext.tblInvoiceItems
on invoice.InvoiceId equals item.InvoiceId
join product in DbContext.tblProductMasters
on item.ProductId equals product.ProductId
//here I was having issue for group by columns from multiple tables
group new { company, invoice, item, product } by new
{
invoice.InvoiceNo,
invoice.InvoiceDate,
invoice.InvoiceId,
invoice.InvoiceType,
company.CompanyId,
company.CompanyName,
} into g
select new
{
g.Key.CompanyId,
g.Key.CompanyName,
g.Key.InvoiceNo,
g.Key.InvoiceType,
g.Key.InvoiceId,
g.Key.InvoiceDate,
Sum = g.Sum(o => o.item.Quantity * o.product.Rate)
}).ToList();
我被困在多列组中,它们来自不同的表,在执行乘法时进行求和,例如:
SUM(tblInvoiceItem.Quantity * tblProductMaster.Rate)
编辑:注释中也建议不要在linq中使用这种连接查询,而是使用导航属性以获得更好的最终结果
group new { company, invoice, product } by new
{
invoice.InvoiceId,
company.CompanyId
} into g
select new
{
Sum = g.Sum(o => o.invoice.Quantity * o.product.Rate)
}
等等
以此类推……group子句返回一系列IGROUP对象,这些对象包含零个或多个与组的键值匹配的项。每个iGroup对象都有一个Key属性,其中存储了键值:
//...
group new { company, invoice } by new
{
invoice.InvoiceNo,
invoice.InvoiceDate,
invoice.InvoiceId,
invoice.InvoiceType,
company.CompanyId,
company.CompanyName,
} into g
select new
{
g.Key.CompanyId,
g.Key.CompanyName,
g.Key.InvoiceNo,
g.Key.InvoiceType,
g.Key.InvoiceId,
Sum = g.Sum(o => o.invoice.Quantity * o.company.Rate)
};
group子句返回一系列IGROUP对象,这些对象包含零个或多个与组的键值匹配的项。每个iGroup对象都有一个Key属性,其中存储了键值:
//...
group new { company, invoice } by new
{
invoice.InvoiceNo,
invoice.InvoiceDate,
invoice.InvoiceId,
invoice.InvoiceType,
company.CompanyId,
company.CompanyName,
} into g
select new
{
g.Key.CompanyId,
g.Key.CompanyName,
g.Key.InvoiceNo,
g.Key.InvoiceType,
g.Key.InvoiceId,
Sum = g.Sum(o => o.invoice.Quantity * o.company.Rate)
};
您不必在一条语句中编写整个查询。如果你把它分开,阅读起来会容易得多。顺便说一句,每当我看到人们在LINQ中使用连接时,都会有人告诉我,他们要么没有正确地设计数据模型,要么没有在表和实体之间创建正确的关系。否则,你为什么要加入?哦!我同意前半部分关于中断查询的说法,我想你的意思是用多个作为可查询项来中断查询,下半部分,我如何将所有这些数据保存在一个表中,公司中只有一个记录,每个公司可以有多个发票,每个发票可以有多个发票项,我很高兴知道在这个设计中是否有改进的地方我不确定你们这里的实际问题是什么?任何错误或异常?导航属性在哪里?您确实应该开始使用导航属性。这些冗长的连接很笨重,容易出错,并且大大降低了代码的可读性。如果你把它分开,阅读起来会容易得多。顺便说一句,每当我看到人们在LINQ中使用连接时,都会有人告诉我,他们要么没有正确地设计数据模型,要么没有在表和实体之间创建正确的关系。否则,你为什么要加入?哦!我同意前半部分关于中断查询的说法,我想你的意思是用多个作为可查询项来中断查询,下半部分,我如何将所有这些数据保存在一个表中,公司中只有一个记录,每个公司可以有多个发票,每个发票可以有多个发票项,我很高兴知道在这个设计中是否有改进的地方我不确定你们这里的实际问题是什么?任何错误或异常?导航属性在哪里?您确实应该开始使用导航属性。这些冗长的连接笨重、容易出错,并大大降低了代码的可读性。谢谢,这两方面都有帮助,我已经更新了已损坏的查询,现在当我着手选择查询的新部分时,公司和发票变量不在上下文中,因此获取相同的错误。@MohammedDawoodAnsari只需在groupping查询中添加产品并使用它。更新的回答谢谢,这两件事都有帮助,我已经更新了坏掉的查询,现在当我选择查询的新部分时,公司和发票变量不在上下文中,并且得到了相同的错误。@MohammedDawoodAnsari只是在groupping查询中添加产品并使用它。更新的答案完美的答案,我现在得到相同的输出。非常感谢你。上帝保佑你。完美的答案,我现在得到了同样的结果。非常感谢你。上帝保佑你。