[英]LINQ with JOIN, GROUP, SUM() and return a new SELECT
it is possible to perform a LINQ like this SQL:可以像这样的 SQL 执行 LINQ:
select invoice.credit, ((sum(detailsInvoice.amount) - sum(detailsInvoice.discount))+ sum(detailsInvoice.tax)) as total
from detailsInvoice
join invoice on detailsInvoice.invoiceID = invoice.ID
where invoice.carga = 1428 and invoice.ID <> 0
group by invoice.credit
I'm getting this result in the SQL我在 SQL 中得到这个结果
I spend so much time trying to create a LINQ to perform that query, without luck.我花了很多时间试图创建一个 LINQ 来执行该查询,但没有运气。
you should do something like this, please excuse any typo errors, didn't use a compiler to test it.你应该做这样的事情,请原谅任何错字错误,没有使用编译器来测试它。
var result = db.invoice.Include(x=>x.detailsInvoice).
GroupBy(x=>invoice.credit).
Select(y=> new {
credit = y.Key,
Total = (y.Sum(z=>z.detailsInvoice.amount) - y.Sum(z=>z.detailsInvoice.discount) + y.Sum(z=>detailsInvoice.tax))
});
Hope this helps!希望这可以帮助!
your answer gave me a good idea of what to do.你的回答让我知道该怎么做。 something important I forgot to mention is the Invoice table and detailsInvoice don't have a relationship.我忘记提及的重要一点是 Invoice 表和 detailsInvoice 没有关系。
So this is what I did:所以这就是我所做的:
var result = (from _invoice in ruterosContext.Invoice
join _details in ruterosContext.DetailsInvoice
on new { id = _invoice.ID, reference = _invoice.Reference } equals
new { id = _details.invoiceID, reference = _details.Reference }
where _invoice.ID != 0 && _invoice.Carga == 1428
select new {
Credit = _invoice.credit,
amount = _details.amount,
discount = _details.discount,
tax = _details.tax
}).GroupBy(x => x.credit).
Select(y => new { Credit = y.Key,
Total = (y.Sum(z => z.amount) - y.Sum(z => z.discount)) + y.Sum(x => x.tax) });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.