我有Sale表和支付表我可以使用下面的sql检索数据,但同样的结果无法生成使用实体框架
select m.id,Sum(p.PaymentAmount),
InvoiceAmount FROM Sale m
Left Join Payment p on m.id =p.saleid
Where m.DueDate < getdate()
Group by m.id,InvoiceAmount
having ISNULL(Sum(p.PaymentAmount),0) < InvoiceAmount
我已经返回查询但是它没有工作
_salesRepository
.GetAll()
.Where(m => m.DueDate < DateTime.Now && m.Payment.GroupBy(o => o.SaleID).Select(g => new { TotalSale = g.Sum(i => i.PaymentAmount) }).Any(g => g.TotalSale < m.InvoiceAmount))
答案 0 :(得分:0)
试试这个:
var getdate = DateTime.Now;
var data = (from s in _salesRepository.GetAll()
join p in _paymentsRepository.GetAll()
on s.id == p.saleid into subPs
from subP in subPs.DefaultIfEmpty()
where s.DueDate < getdate
group new { s, subP } by new { s.id, s.InvoiceAmount } into leftSub
let PaymentAmount = leftSub.Sum(x => x.subP == null ? 0 : x.subP.PaymentAmount)
where PaymentAmount < leftSub.Key.InvoiceAmount
select new {
leftSub.Key.id,
PaymentAmount,
leftSub.Key.InvoiceAmount
}).ToList();