Multiple Sum using Linq (lambda expression)

Its very straight forward, I'm trying to Sum both Amount + Adjusted

I'm sure its not the best approach to use.

var a1 = context.Employee.Select( a => a.Amount).ToList().Sum();
var b1 = context.Employee.Select(b => b.Adjusted).ToList().Sum();
var c1 = a1+b1;
Jon Skeet
people
quotationmark

There's no need to create any lists (which will pull a load of data from the database, if this is LINQ to SQL or EF) - you can just use:

var c1 = context.Employee.Sum(x => x.Amount + x.Adjusted);

That's assuming you don't need the individual sums. If you do, I'd probably use:

var a1 = context.Employee.Sum(x => x.Amount);
var b1 = context.Employee.Sum(x => x.Adjusted);
var c1 = a1 + b1;

That's two calls, admittedly. This can be done with Aggregate, but it would definitely be uglier.

var both = context.Employee.Aggregate(
       new { Amount = 0, Adjusted = 0 }, // Seed
       (pair, emp) => new { pair.Amount + emp.Amount,
                            pair.Adjusted + emp.Adjusted });
var a1 = both.Amount;
var b1 = both.Adjusted;
var c1 = a1 + b1;

people

See more on this question at Stackoverflow