Get sum of two columns in one LINQ query
How about
m.Items.Select(item => new { Total = item.Total, Done = item.Done })
.Aggregate((t1, t2) => new { Total = t1.Total + t2.Total, Done = t1.Done + t2.Done });
To sum the table, group by a constant:
from p in m.Items
group p by 1 into g
select new {
SumTotal = g.Sum(x => x.Total),
SumDone = g.Sum(x => x.Done)
}
This will do the trick:
LINQ Query Syntax:
from p in m.Items
group p by 1 into g
select new
{
SumTotal = g.Sum(x => x.Total),
SumDone = g.Sum(x => x.Done)
};
LINQ Method Syntax:
m.Items
.GroupBy(r => 1)
.Select(g => new
{
SumTotal = g.Sum(x => x.Total),
SumDone = g.Sum(x => x.Done)
});
Notice that this solution (both syntax) returns a list of 1 item, so you might want to add at the end:
.FirstOrDefault();