如何在LINQ中分组?

时间:2013-07-30 16:38:31

标签: c# .net linq entity-framework linq-to-sql

我需要返回特定用户每日约会的最后30天,并检查用户是否每天至少预约8小时。

在sql中我可以使用此命令执行此操作:

select IDAppointment,IDUser, SUM(DurationInHours) from Note where AppointmentDate > *lastmonth and IDUser = @userID group by IDUser,IDAppointment,AppointmentDate   

然后我得到结果并验证DurationInHours(双重类型)。

使用LINQ可以做到吗? 获取上个月用户约会的列表并逐日验证

谢谢!

2 个答案:

答案 0 :(得分:10)

这应该大致存在,尽管这不是我的头脑,而不是在IDE。

var result = context.Notes
                    .Where(n => [Your where clause])
                    .GroupBy(n => new { n.IDUser, n.IDAppointment, n.AppointmentDate})
                    .Select(g => new {
                                   g.Key.IDAppointment,
                                   g.Key.IDUser,
                                   g.Sum(n => n.DurationInHours)});

<强>更新

作为参考,你的where子句将是这样的......(再次在我的头顶)

DateTime lastMonth = DateTime.Today.AddMonths(-1);
int userId = 1 // TODO: FIX
var result = context.Notes.Where(n => n.AppointmentDate > lastMonth
                                   && n.IDUser = userId)

导致......

DateTime lastMonth = DateTime.Today.AddMonths(-1);
int userId = 1 // TODO: FIX
var result = context.Notes
                    .Where(n => n.AppointmentDate > lastMonth
                             && n.IDUser = userId)
                    .GroupBy(n => new { n.IDUser, n.IDAppointment, n.AppointmentDate})
                    .Select(g => new {
                                   g.Key.IDAppointment,
                                   g.Key.IDUser,
                                   g.Sum(n => n.DurationInHours)});

答案 1 :(得分:2)

这是我测试过的解决方案。

DateTime lastMonth = DateTime.Today.AddMonths(-1);
int selectedUserId = 2; 

var notes = new List<Note>(
    new Note[] {
        new Note() { 
            AppointmentDate = new DateTime(2013,7,30){}, 
            IDAppointment = 1, IDUser = 1, DurationInHours = 1
        },
        new Note() {
            AppointmentDate = new DateTime(2013,7,30){}, 
            IDAppointment = 1, IDUser = 1, DurationInHours = 2
        },
        new Note() {
            AppointmentDate = new DateTime(2013,7,30){}, 
            IDAppointment = 1, IDUser = 1, DurationInHours = 3
        },
        new Note() {
            AppointmentDate = new DateTime(2013,7,28){}, 
            IDAppointment = 2, IDUser =  2, DurationInHours = 2
        },
        new Note() {
            AppointmentDate = new DateTime(2013,7,28){}, 
            IDAppointment = 2, IDUser =  2, DurationInHours = 3
        },
        new Note() {
            AppointmentDate = new DateTime(2013,7,27){}, 
            IDAppointment = 2, IDUser =  2, DurationI nHours = 4
        },
        new Note() {
            AppointmentDate = new DateTime(2013,7,26){}, 
            IDAppointment = 3, IDUser =  3, DurationInHours = 3
        },
        new Note() {
            AppointmentDate = new DateTime(2013,7,25){}, 
            IDAppointment = 3, IDUser =  3, DurationInHours = 4
        },
        new Note() {
            AppointmentDate = new DateTime(2013,7,24){}, 
            IDAppointment = 3, IDUser =  3, DurationInHours = 5
        }
    }
);

var results = from n in notes
              group n by new {n.IDUser, n.IDAppointment, n.AppointmentDate}
              into g
              where g.Key.AppointmentDate > lastMonth && 
                    g.Key.IDUser == selectedUserId
              select new {
                  g.Key.IDAppointment, 
                  g.Key.IDUser, 
                  TotalHours = g.Sum(n => n.DurationInHours)
              };

summation属性需要明确地给出一个名称(即TotalHours),否则你会得到错误CS0746:无效的匿名类型成员声明符。必须使用成员分配,简单名称或成员访问权声明匿名类型成员。