1

私のユーザーアクティビティログは次のようになります。

ID、ユーザー名、日付

1日あたりのエントリの総数と、過去10日間のその日の最もアクティブなユーザーを計算する必要があります。

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

LINQとSQLを初めて使用する場合、誰かがこのクエリを完了するのを手伝ってくれますか?

4

2 に答える 2

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 
于 2012-06-08T07:54:57.793 に答える
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);
于 2012-06-08T07:36:08.530 に答える