2008-12-16 46 views
2

我想转换我的一些存储过程LINQ和我遇到了下列Transact-SQL语句的问题:试图在Linq中做一个子查询...有问题!

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, 
} 
+0

你能解释一下你遇到的问题/错误? – 2008-12-16 14:13:29

+0

我想转换这个Linq ...这是我挣扎的地方...主要是与子查询。 – mattruma 2008-12-16 14:14:44

回答

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) 
}