views:

493

answers:

2

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()
 }


+2  A: 

Would this work:

var answer1Sum = SurveyAnswers.Sum( survey => survey.Answer1 );
var answer2Sum = SurveyAnswers.Sum( survey => survey.Answer2 );
var answer3Sum = SurveyAnswers.Sum( survey => survey.Answer3 );
Alex Black
If he wants it all in one object, you can basically wrap that with a new{...}. (Not saying he does, just pointing out an option)
Ryan Versaw
Thanks everyone!
A: 
SurveyAnswers.Sum(r => r.Answer1);
SLaks