9

我有一个使用 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; }
    }
4

2 回答 2

15

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

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

于 2012-07-04T21:10:22.963 回答
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;
}
于 2012-07-06T05:24:46.113 回答