2

我试图弄清楚如何编写一个 linq 查询,它将等效的结果返回给下面的 sql 查询。我遇到的问题与主查询的选择列表中包含的两个选择计数查询有关。我需要从PaymentHistory表中计算去年的两种不同类型的记录。可以使用 linq 编写等效的内容吗?最好使用 lambda 语法。

select ieinum, serviceaddrkey,
  (select count(*) from PaymentHistory where serviceaddrid = serviceaddrkey
   and PostDate >= DateAdd(year, -1 , GetDate())
   and SetID = 100) as ReturnedFees,
(select count(*) from PaymentHistory where serviceaddrid = serviceaddrkey
   and PostDate >= DateAdd(year, -1 , GetDate())
   and SetID = 101) as CorrectedReturnedFees
from Serviceaddr 

任何帮助都会很棒。

4

1 回答 1

0

也许是这样的:

from s in Serviceaddr
let ph = PaymentHistory.Where(p => s.serviceaddrkey == p.serviceaddrkey &&
                                   p.PostDate >= DateTime.Now.AddYears(-1))
select new
{
  s.ieinum,
  s.serviceaddrkey,
  ReturnedFees = ph.Count(p => p.SetID == 100),
  CorrectedReturnedFees = ph.Count(p => p.SetID == 101)
}
于 2013-05-23T18:27:02.510 回答