Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用LINQ计算用户统计信息_C#_Sql_Linq - Fatal编程技术网

C# 使用LINQ计算用户统计信息

C# 使用LINQ计算用户统计信息,c#,sql,linq,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的新手,有人能帮我完成这个查询吗?应该可以。这将从过去10天中选出最热门的用户 var query = (from us

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

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的新手,有人能帮我完成这个查询吗?

应该可以。这将从过去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);

这应该行得通。这将从过去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);

我想这就是你想要的。只要把它放到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 
void Main()
{
var日志=新列表
{
新用户日志{Id=1,Date=new DateTime(2012,1,1),Username=“cburgdorf”},
新用户日志{Id=2,Date=new DateTime(2012,1,1),Username=“cburgdorf”},
新用户日志{Id=3,Date=new DateTime(2012,1,1),Username=“cburgdorf”},
新用户日志{Id=4,Date=new DateTime(2012,1,1),Username=“Mister Foo”},
新用户日志{Id=5,Date=new DateTime(2012,1,1),Username=“Mister Foo”},
新用户日志{Id=6,Date=new DateTime(2012,1,2),Username=“Mister Bar”},
新用户日志{Id=7,Date=new DateTime(2012,1,2),Username=“Mister Bar”},
新用户日志{Id=8,Date=new DateTime(2012,1,2),Username=“cburgdorf”},
新用户日志{Id=9,Date=new DateTime(2012,1,2),Username=“Mister Foo”},
新用户日志{Id=10,Date=new DateTime(2012,1,2),Username=“Mister Foo”},
新用户日志{Id=11,Date=new DateTime(2012,1,2),Username=“Mister Foo”},
新用户日志{Id=12,Date=new DateTime(2012,1,2),Username=“Mister Bar”}
};
日志
.OrderByDescending(l=>l.Date)
.GroupBy(log=>log.Date)
.选择(日志=>log
.GroupBy(l=>l.Username)
.选择(l=>new
{
Count=l.Count(),
Value=l.FirstOrDefault(),
})
.OrderBy(l=>l.Count).Last())
.选择(日志=>新建)
{
日期=log.Value.Date,
Count=log.Count,
Username=log.Value.Username
})
.Take(10)
.Dump();
//在LINQPad中,使用Dump()查看结果:
/*
日志
.OrderByDescending(l=>l.Date)
.GroupBy(log=>log.Date)
.选择(日志=>log
.GroupBy(l=>l.Username)
.选择(l=>new
{
Count=l.Count(),
Value=l.FirstOrDefault(),
})
.OrderBy(l=>l.Count).Last())
.选择(日志=>新建)
{
日期=log.Value.Date,
Count=log.Count,
Username=log.Value.Username
})
.Take(10)
.Dump();
*/
}
类用户日志
{
公共int Id{get;set;}
公共日期时间日期{get;set;}
公共字符串用户名{get;set;}
}
结果是:
2012年1月2日00:00:00 | 3 |傅先生
2012年1月1日00:00:00 | 3 | cburgdorf

我想这就是你想要的。只要把它放到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 
void Main()
{
var日志=新列表
{
新用户日志{Id=1,Date=new DateTime(2012,1,1),Username=“cburgdorf”},
新用户日志{Id=2,Date=new DateTime(2012,1,1),Username=“cburgdorf”},
新用户日志{Id=3,Date=new DateTime(2012,1,1),Username=“cburgdorf”},
新用户日志{Id=4,Date=new DateTime(2012,1,1),Username=“Mister Foo”},
新用户日志{Id=5,Date=new DateTime(2012,1,1),Username=“Mister Foo”},
新用户日志{Id=6,Date=new DateTime(2012,1,2),Username=“Mister Bar”},
新用户日志{Id=7,Date=new DateTime(2012,1,2),Username=“Mister Bar”},
新用户日志{Id=8,Date=new DateTime(2012,1,2),Username=“cburgdorf”},
新用户日志{Id=9,Date=new DateTime(2012,1,2),Username=“Mister Foo”},
新用户日志{Id=10,Date=new DateTime(2012,1,2),Username=“Mister Foo”},
新用户日志{Id=11,Date=new DateTime(2012,1,2),Username=“Mister Foo”},
新用户日志{Id=12,Date=new DateTime(2012,1,2),Username=“Mister Bar”}
};
日志
.OrderByDescending(l=>l.Date)
.GroupBy(log=>log.Date)
.选择(日志=>log
.GroupBy(l=>l.Username)
.选择(l=>new
{
Count=l.Count(),
Value=l.FirstOrDefault(),
})
.OrderBy(l=>l.Count).Last())
.选择(日志=>新建)
{
日期=log.Value.Date,
Count=log.Count,
Username=log.Value.Username
})
.Take(10)
.Dump();
//在LINQPad中,使用Dump()查看结果:
/*
日志
.OrderByDescending(l=>l.Date)
.GroupBy(log=>log.Date)
.选择(日志=>log
.GroupBy(l=>l.Username)
.选择(l=>new
{
Count=l.Count(),
Value=l.FirstOrDefault(),
})
.OrderBy(l=>l.Count).Last())
.选择(日志=>新建)
{
日期=log.Value.Date,
Count=log.Count,