使用LINQ计算用户统计信息

时间:2012-06-08 06:04:33

标签: c# sql linq

我的用户活动日志如下所示:

ID,用户名,日期

我需要计算每天的总条目数,以及过去10天当天最活跃的用户数。

//pseudo code
from entry in data.UserLogs
group by entry.Date == each day
select username of most active user, count(Id)

我是LINQ和SQL的新手,有人可以帮我完成这个查询吗?

2 个答案:

答案 0 :(得分:1)

我认为这就是你所追求的。只需将其放入LINQPad即可看到它的实际效果

void Main()
{
    var logs = new List<UserLog>
        {
            new UserLog { Id= 1, Date = new DateTime(2012,1,1), Username = "cburgdorf"},
            new UserLog { Id= 2, Date = new DateTime(2012,1,1), Username = "cburgdorf"},
            new UserLog { Id= 3, Date = new DateTime(2012,1,1), Username = "cburgdorf"},
            new UserLog { Id= 4, Date = new DateTime(2012,1,1), Username = "Mister Foo"},
            new UserLog { Id= 5, Date = new DateTime(2012,1,1), Username = "Mister Foo"},
            new UserLog { Id= 6, Date = new DateTime(2012,1,2), Username = "Mister Bar"},
            new UserLog { Id= 7, Date = new DateTime(2012,1,2), Username = "Mister Bar"},
            new UserLog { Id= 8, Date = new DateTime(2012,1,2), Username = "cburgdorf"},
            new UserLog { Id= 9, Date = new DateTime(2012,1,2), Username = "Mister Foo"},
            new UserLog { Id= 10, Date = new DateTime(2012,1,2), Username = "Mister Foo"},
            new UserLog { Id= 11, Date = new DateTime(2012,1,2), Username = "Mister Foo"},
            new UserLog { Id= 12, Date = new DateTime(2012,1,2), Username = "Mister Bar"}
        };

    logs
        .OrderByDescending (l => l.Date)
        .GroupBy (log => log.Date)      
        .Select (log => log
                        .GroupBy (l => l.Username)
                        .Select (l => new 
                        {
                            Count = l.Count (),
                            Value = l.FirstOrDefault (),
                        })
                        .OrderBy (l => l.Count).Last ())
        .Select (log => new 
        {
            Date = log.Value.Date,
            Count = log.Count,
            Username = log.Value.Username
        })
        .Take(10)
        .Dump();

        //In LINQPad use Dump() to see the results:
        /*
            logs
                .OrderByDescending (l => l.Date)
                .GroupBy (log => log.Date)      
                .Select (log => log
                                .GroupBy (l => l.Username)
                                .Select (l => new 
                                {
                                    Count = l.Count (),
                                    Value = l.FirstOrDefault (),
                                })
                                .OrderBy (l => l.Count).Last ())
                .Select (log => new 
                {
                    Date = log.Value.Date,
                    Count = log.Count,
                    Username = log.Value.Username
                })
                .Take(10)
                .Dump();
        */


}

class UserLog
{
    public int Id {get;set;}
    public DateTime Date {get;set;}
    public string Username {get;set;}
}


The result is:

    02.01.2012 00:00:00 | 3 | Mister Foo 
    01.01.2012 00:00:00 | 3 | cburgdorf 

答案 1 :(得分:0)

这应该有效。这将选择过去10天内的最高用户。

var query =
   (from userLog in data.UserLogs
    group userLog.UserName by userLog.Date.Date into usersPerDate
    orderby usersPerDate.Key descending
    let topUsers =
        from user in usersPerDate
        group 1 by user into g
        let count = g.Count()
        orderby count descending
        select new
        {
            UserName = g.Key,
            Count = count,
        }
    select topUsers.First()).Take(10);