Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way to call .Sum() on multiple properties

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

Edit

private IQueryable<OminitureStats> GetOmnitureDataAsQueryable(int? fnsId, int dateRange)
    {
        var yesterday = DateTime.Today.AddDays(-1);
        var nDays = yesterday.AddDays(-dateRange);

        if (fnsId.HasValue)
        {
            IQueryable<OminitureStats> query = from o in lhDB.omniture_stats
                                               where o.fns_id == fnsId
                                                     && o.date <= yesterday
                                                     && o.date > nDays
                                               select new OminitureStats ( 
                                                   o.page_views.GetValueOrDefault(), 
                                                   o.monthly_unique.GetValueOrDefault(),
                                                   o.visits.GetValueOrDefault(),
                                                   (double)o.bounce_rate.GetValueOrDefault()
                                               );
            return query;
        }
        return null;
    }

Edit:

public class OminitureStats
    {
        public OminitureStats(int PageViews, int MonthlyUniqueVisitors, int Visits, double BounceRate)
        {
            this.PageViews = PageViews;
            this.MonthlyUniqueVisitors = MonthlyUniqueVisitors;
            this.Visits = Visits;
            this.BounceRate = BounceRate;
            this.PagesPerVisit = Math.Round((double)(PageViews / Visits), 1);
        }

        public OminitureStats(int PageViews, int MonthlyUniqueVisitors, int Visits, double BounceRate, double PagesPerVisit)
        {
            this.PageViews = PageViews;
            this.MonthlyUniqueVisitors = MonthlyUniqueVisitors;
            this.Visits = Visits;
            this.BounceRate = BounceRate;
            this.PagesPerVisit = PagesPerVisit;
        }

        public int PageViews { get; set; }
        public int MonthlyUniqueVisitors { get; set; }
        public int Visits { get; set; }
        public double PagesPerVisit { get; set; }
        public double BounceRate { get; set; }
    }
like image 852
SherCoder Avatar asked Jul 04 '12 21:07

SherCoder


1 Answers

IIRC you can do all the sums in one go (as long as the query is translated to SQL) with

var sums = query.GroupBy(q => 1)
                .Select(g => new
                {
                    PageViews = g.Sum(q => q.PageViews),
                    Visits = g.Sum(q => q.Visits),
                    // etc etc
                })
                .Single();

This will give you one object which contains all the sums as separate properties.

like image 102
Jon Avatar answered Nov 09 '22 21:11

Jon