在多个属性上调用.Sum()的有效方法

时间:2012-07-04 21:01:34

标签: c# linq iqueryable

我有一个使用Linq从数据库中获取数据的函数,然后我在另一个函数中调用该函数,在每个单独的属性上使用.Sum对所有单个属性求和。我想知道是否有一种有效的方法可以立即对所有属性求和,而不是在每个属性上调用.Sum()。我认为我现在正在做的方式非常缓慢(虽然未经测试)。

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

修改

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

修改

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

2 个答案:

答案 0 :(得分:9)

IIRC你可以一次性完成所有的总和(只要查询被翻译成SQL)

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

这将为您提供一个对象,其中包含所有总和作为单独的属性。

答案 1 :(得分:0)

我发现了为什么抛出NotSupportedException。我了解到Linq to Entity不支持带参数的构造函数,因此删除了构造函数并在查询中进行了更改。我是C#程序员的新手,所以如果我的解决方案可以改进,请告诉我,但是现在它的工作正常。

public class OminitureStats
{
    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; }
}


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