试图在Linq中做一个子查询...有问题!

时间:2008-12-16 14:08:49

标签: database linq linq-to-sql tsql

我正在尝试将一些存储过程转换为Linq,并且遇到以下Transact-Sql语句的问题:

Select 
    Year(p.StartDate) As Year,
    (Select Sum(t.Units) From Time t Where Year(t.TransactionDate) = Year(p.StartDate)) As Hours,
    (Select Sum(i.Price) From Invoice i Where Year(i.CreatedDate) = Year(p.StartDate)) As Invoices
From 
    Period p
Group By
    Year(p.StartDate)
Order By
    Year(p.StartDate)

我正在与LinqPad合作试图解决这个问题......任何帮助都将不胜感激!

进度

到目前为止,我有以下内容...只是想了解如何转换子查询:

from p in Periods
group p by p.StartDate.Year into g
orderby g.Key
select new 
{
    g.Key,
}

1 个答案:

答案 0 :(得分:5)

尝试:

from p in Periods
group p by p.StartDate.Year into g
orderby g.Key
select new
{
g.Key,
Hours = (from t in Times where t.TransactionDate.Year == g.Key select t).Sum(t => t.Units),
Invoices = (from i in Invoices where i.CreatedDate.Year == g.Key select i).Sum(i => i.Price)
}