LINQ | 如何在不分组的情况下获得 SUM?

发布于 2024-07-26 03:04:19 字数 543 浏览 5 评论 0原文

疯狂的问题...但是,我想要表中某一列的所有行的总和(不使用 group by 子句)

示例:

Table = Survey
Columns = Answer1, Answer2, Answer3
        1        1         1
        4        3         5
        3        3         2

我想要每列的总和。

最终结果应如下所示:

Answer1Sum  Answer2Sum  Answer2Sum

8           7           8

这不起作用:

from survey in SurveyAnswers
select new
{
    Answer1Sum = survey.Sum(),
    Answer2Sum = survey.Sum(),
    Answer3Sum = survey.Sum()
 }

Crazy question...however, I want the sum of all the rows in a table for a column (without using the group by clause)

Example:

Table = Survey
Columns = Answer1, Answer2, Answer3
        1        1         1
        4        3         5
        3        3         2

I want the sums for each column.

Final results should look like:

Answer1Sum  Answer2Sum  Answer2Sum

8           7           8

This doesn't work:

from survey in SurveyAnswers
select new
{
    Answer1Sum = survey.Sum(),
    Answer2Sum = survey.Sum(),
    Answer3Sum = survey.Sum()
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

无所谓啦 2024-08-02 03:04:19

这行得通吗:

var answer1Sum = SurveyAnswers.Sum( survey => survey.Answer1 );
var answer2Sum = SurveyAnswers.Sum( survey => survey.Answer2 );
var answer3Sum = SurveyAnswers.Sum( survey => survey.Answer3 );

Would this work:

var answer1Sum = SurveyAnswers.Sum( survey => survey.Answer1 );
var answer2Sum = SurveyAnswers.Sum( survey => survey.Answer2 );
var answer3Sum = SurveyAnswers.Sum( survey => survey.Answer3 );
撑一把青伞 2024-08-02 03:04:19

对于任何需要它的人来说,这个答案的 VB.NET 解决方案如下:

Dim Answer1Sum = SurveyAnswers.Sum(Function(survey) survey.Answer1)
Dim Answer2Sum = SurveyAnswers.Sum(Function(survey) survey.Answer2)
Dim Answer3Sum = SurveyAnswers.Sum(Function(survey) survey.Answer3)

A VB.NET soltuion to this answer for anyone that needs it is as follows:

Dim Answer1Sum = SurveyAnswers.Sum(Function(survey) survey.Answer1)
Dim Answer2Sum = SurveyAnswers.Sum(Function(survey) survey.Answer2)
Dim Answer3Sum = SurveyAnswers.Sum(Function(survey) survey.Answer3)
滥情稳全场 2024-08-02 03:04:19
SurveyAnswers.Sum(r => r.Answer1);
SurveyAnswers.Sum(r => r.Answer1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文