C#linq使用SUM查询OUTER APPLY

时间:2015-08-13 19:52:09

标签: linq sum apply outer-join

尝试将SQL查询转换为Linq:

SELECT GrossInvoiceAmount, C.SumOfPayments
  FROM invoice
OUTER APPLY(
  SELECT SUM(SumOfPayments) AS SumOfPayments FROM vw_sumOfPayments
  WHERE vw_sumOfPayments.PaymentDate = '01/01/2010 00:00:00'
      AND vw_sumOfPayments.InvoiceId = invoice.InvoiceId
) C
WHERE LastTransmitDate = '01/01/2010 00:00:00'

我的C#代码中有这个。它运行并给出结果,但invoice.GrossInvoiceAmount数量是错误的。有任何想法吗?感谢。

var InvoiceQuery = (from invoice in this.Context.Invoices
        join payment in this.Context.vw_sumOfPayments.Where(pay => DbFunctions.TruncateTime(pay.PaymentDate) >= startdate 
                                        && DbFunctions.TruncateTime(pay.PaymentDate) <= enddate) 
                on invoice.InvoiceID equals payment.InvoiceID into pays
        select new InvoiceModel{

           InvoiceTypeId = invoice.InvoiceTypeId,
           InvoiceNumber = invoice.InvoiceNumber,
           InvoiceDate = invoice.InvoiceDate,
           InvoiceAmount = invoice.GrossInvoiceAmount,
           PaymentAmount = pays.AsEnumerable().Sum(o => o.SumOfPayments)

         });

0 个答案:

没有答案