如何从linq查询中的两个不同分组的iqueryable对象计数?

时间:2013-04-23 10:10:54

标签: c# linq-to-entities

目前我正在使用以下查询来计算用户活动统计信息,例如用户登录,退出等多少次......其工作正常。但是我想通过仅创建一次UserActivityStatistics对象并希望避免重复代码来改进我的代码。创建对象两次,如下所示。

    private IList<UserActivityStatistics> ToUserActivityStatistics(DateTime fromDate, DateTime toDate, int pageIndex, int pageSize, Guid userId, ActivityType activity)
    {
        IList<UserActivityStatistics> userActivityStatistics = null;

        if (userId == null || userId == Guid.Empty)
        {
            userActivityStatistics = (from u in SqlGetUserActivityStatistics(fromDate, toDate, userId, activity)
                                      group u by new { u.Activity, u.UserId } into g
                                      select g).AsEnumerable().Select(x => new UserActivityStatistics
                                      {
                                          Activity = Activity.GetActivityTypeName((ActivityType)x.Key.Activity),
                                          ActivityCount = x.Count(),
                                          UserId = x.Key.UserId,
                                          UserName = x.Select(y => y.aspnet_Users.UserProfiles.FirstName + " " + y.aspnet_Users.UserProfiles.LastName).FirstOrDefault()

                                      }).OrderByDescending(y => y.ActivityCount).Skip(pageIndex).Take(pageSize).ToList();
        }
        else
        {

            userActivityStatistics = (from u in SqlGetUserActivityStatistics(fromDate, toDate, userId, activity)
                                      group u by u.Activity into g
                                      select g).AsEnumerable().Select(x => new UserActivityStatistics
                                      {
                                          Activity = Activity.GetActivityTypeName((ActivityType)x.Key),
                                          ActivityCount = x.Count(),
                                          UserId = x.Select(y => y.aspnet_Users.UserProfiles.UserId).FirstOrDefault(),
                                          UserName = x.Select(y => y.aspnet_Users.UserProfiles.FirstName + " " + y.aspnet_Users.UserProfiles.LastName).FirstOrDefault()
                                      }).OrderByDescending(z => z.ActivityCount).Skip(pageIndex).Take(pageSize).ToList();

        }

        return userActivityStatistics;
    }

我正在改进我的代码,如下所示,因为我将UserActivityStatistics对象移到外面,并将其分组为可查询的。但是我无法从该iqueryable对象计算计数。我在网上搜索,但我没有得到任何解决方案。

希望清楚......

    private IList<UserActivityStatistics> ToUserActivityStatistics(DateTime fromDate, DateTime toDate, int pageIndex, int pageSize, Guid userId, ActivityType activity)
    {
        IList<UserActivityStatistics> userActivityStatistics = null;
        IQueryable<ActivityLog> activityLog = null;

        if (userId == null || userId == Guid.Empty)
        {

            activityLog = (from u in SqlGetUserActivityStatistics(fromDate, toDate, userId, activity)
                            select u).GroupBy(x => new { x.Activity, x.UserId }).SelectMany(x => x);


        }
        else
        {
            activityLog = (from u in SqlGetUserActivityStatistics(fromDate, toDate, userId, activity)
                            select u).GroupBy(x => new { x.Activity}).SelectMany(x => x);
        }


        userActivityStatistics = (from u in activityLog
                                  select u).AsEnumerable().Select(x => new UserActivityStatistics
                                 {
                                     Activity = Activity.GetActivityTypeName((ActivityType)x.Activity),
                                     ActivityCount = //unable to do count
                                     UserId = x.UserId
                                 }).OrderByDescending(x => x.ActivityCount).Skip(pageIndex).Take(pageSize).ToList();


        return userActivityStatistics;
    }

0 个答案:

没有答案