分组依据、求和、加入 Linq
我有这个简单的 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
。
对此的任何帮助将不胜感激。 谢谢。
I have this simple sql query:
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
and I am trying to convert it to work in linq like this:
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)
}
but when I try to run it in LINQPad I get the following message:
The name 'c' does not exist in the
current context
and it highlights c.Lastname
in select new
statement.
Any help on this will be greatly appreciated.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,您已按
c.LastName
对b
进行分组。因此,在分组操作之后,您将处理g
,它是一个分组,其中 element 类型是b
的类型,并且 < em>key 类型是c.LastName
的类型。您很可能需要的是:...但是如果您需要了解
c
的任何其他方面,则需要更改分组表达式。Well, you've grouped
b
byc.LastName
. So after the grouping operation, you're dealing withg
which is a grouping with the element type being the type ofb
, and the key type being the type ofc.LastName
. It could well be that all you need is:... but if you need to get at any other aspects of
c
, you'll need to change your grouping expression.