我有这个简单的sql查询:
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'当前上下文
并在select new
语句中突出显示c.Lastname
。
嗯,你已经把b
按c.LastName
分组了。因此,在分组操作之后,您正在处理g
,这是一个分组,元素类型是b
的类型,键类型是c.LastName
的类型。很可能你所需要的只是:
select new {
Name = g.Key,
OpenBalance = g.Sum(t1 => t1.Credit)
}
…但是如果您需要获得c
的任何其他方面,则需要更改分组表达式。