2011-04-05 100 views
0

我有這個簡單的SQL查詢:組,總和,加入Linq中

select c.LastName, Sum(b.Debit)- Sum(b.Credit) as OpenBalance from Balance as b 
inner join Job as j on (b.Job = j.ID) 
inner join Client as c on (j.Client = c.ID) 
Group By c.LastName 

,我試圖將其轉換在LINQ的工作是這樣的:

from b in Balance 
join j in Job on b.Job equals j.ID 
join c in Client on j.Client equals c.ID 
group b by new { c.LastName } into g 
select new { 
    Name = c.Lastname, 
    OpenBalance = g.Sum(t1 => t1.Credit) 
     } 

但是當我嘗試在LINQPad運行我得到以下信息:

名稱「C」不會在 CUR存在租金背景

它突出顯示c.Lastnameselect new聲明。

對此的任何幫助將不勝感激。 謝謝。

回答

4

嗯,你已經bc.LastName分組。因此,在分組操作之後,您正在處理g,它是元素類型b的類型和類型的組合,類型是c.LastName的類型。它很可能是所有你需要的是:

select new { 
    Name = g.Key, 
    OpenBalance = g.Sum(t1 => t1.Credit) 
} 

...但如果你需要獲取的c任何其他方面,則需要更改您的分組表達式。