views:

748

answers:

1

I am trying to convert some of my stored procedures to Linq and am having problems with the following Transact-Sql statement:

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)

I'm working with LinqPad to try to figure this out ... any help would be greatly appreciated!

Progress

I have the following so far ... just trying to figure how to convert the sub queries:

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

Try:

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)
}
jwarzech
That worked! Thank you!
mattruma