[英]SQL to Linq, having trouble in SUM and Multiple column group by with joins
I want below query to be converted into linq for paging in MVC.我希望将下面的查询转换为 linq 以便在 MVC 中进行分页。
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
Equivalent Linq Query:等效的 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();
I am stuck in multiple column GROUP BY
which are from different tables and doing SUM
while performing multiplication, for example:我被困在来自不同表的多列
GROUP BY
,并在执行乘法时执行SUM
,例如:
SUM(tblInvoiceItem.Quantity * tblProductMaster.Rate)
EDIT: Also suggested in the comment, do not use this kind of join queries in linq, instead use navigation properties for the better end result.编辑:评论中还建议,不要在 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)
}
And so on...等等...
The group
clause returns a sequence of IGrouping<TKey, TElement>
objects that contain zero or more items that match the key values for the group. group
子句返回IGrouping<TKey, TElement>
对象的序列,这些对象包含与组的键值匹配的零个或多个项目。 Each IGrouping<TKey, TElement>
object has a Key
property where are stored the key values:每个
IGrouping<TKey, TElement>
对象都有一个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)
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.