4

疯狂的问题......但是,我想要列的表中所有行的总和(不使用 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()
 }

4

3 回答 3

9

这会起作用吗:

var answer1Sum = SurveyAnswers.Sum( survey => survey.Answer1 );
var answer2Sum = SurveyAnswers.Sum( survey => survey.Answer2 );
var answer3Sum = SurveyAnswers.Sum( survey => survey.Answer3 );
于 2009-07-01T20:58:40.603 回答
1

对于任何需要它的人来说,这个答案的 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)
于 2011-01-27T16:43:17.263 回答
0
SurveyAnswers.Sum(r => r.Answer1);
于 2009-07-01T20:59:14.380 回答