Efficient way to call .Sum() on multiple properties
Posted
by
SherCoder
on Stack Overflow
See other posts from Stack Overflow
or by SherCoder
Published on 2012-07-04T21:01:33Z
Indexed on
2012/07/04
21:15 UTC
Read the original article
Hit count: 155
I have a function that uses Linq to get data from the database and then I call that function in another function to sum all the individual properties using .Sum on each individual property. I was wondering if there is an efficient way to sum all the properties at once rather than calling .Sum() on each individual property. I think the way I am doing as of right now, is very slow (although untested).
public OminitureStats GetAvgOmnitureData(int? fnsId, int dateRange)
{
IQueryable<OminitureStats> query = GetOmnitureDataAsQueryable(fnsId, dateRange);
int pageViews = query.Sum(q => q.PageViews);
int monthlyUniqueVisitors = query.Sum(q => q.MonthlyUniqueVisitors);
int visits = query.Sum(q => q.Visits);
double pagesPerVisit = (double)query.Sum(q => q.PagesPerVisit);
double bounceRate = (double)query.Sum(q => q.BounceRate);
return new OminitureStats(pageViews, monthlyUniqueVisitors, visits, bounceRate, pagesPerVisit);
}
© Stack Overflow or respective owner