分组依据、求和、加入 Linq

发布于 2024-10-30 04:57:15 字数 716 浏览 1 评论 0原文

我有这个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼中杀气 2024-11-06 04:57:15

嗯,您已按 c.LastNameb 进行分组。因此,在分组操作之后,您将处理 g,它是一个分组,其中 element 类型是 b 的类型,并且 < em>key 类型是 c.LastName 的类型。您很可能需要的是:

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

...但是如果您需要了解 c 的任何其他方面,则需要更改分组表达式。

Well, you've grouped b by c.LastName. So after the grouping operation, you're dealing with g which is a grouping with the element type being the type of b, and the key type being the type of c.LastName. It could well be that all you need is:

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

... but if you need to get at any other aspects of c, you'll need to change your grouping expression.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文