按日期Linq到MySql组

时间:2014-12-02 13:29:24

标签: c# mysql linq

我想获取一些数据并仅按日期对其进行分组。 Next表达式对我不起作用,EntityFunctions.TruncateTime(users.RegisterTime)inavlid类型的成员声明。

  var result = (from users in userService.GetListSimple()
               .Where(d => d.RegisterTime >= new DateTime(2013, 01, 01) 
                        && d.RegisterTime <= new DateTime(2013, 01, 10,23,59,59))
                group users by new { EntityFunctions.TruncateTime(users.RegisterTime),
                users.partners.Name, users.partners.PartnerId } 
                into gr
                select new
                       {
                         gr.Key.Date,
                         gr.Key.Name,
                         gr.Key.PartnerId,
                         Amount = gr.Count()
                       }
               ).OrderBy(d => d.Date);

如何解决?

我不确定下一个方法是否是最佳的

var data = (from users in userService.GetListSimple()
          .Where(d => d.RegisterTime >= new DateTime(2013, 01, 01) 
                   && d.RegisterTime <= new DateTime(2013, 01, 10, 23, 59, 59))
                  select new
                        {
                            Date = EntityFunctions.TruncateTime(users.RegisterTime),
                            users.partners.Name,
                            users.partners.PartnerId
                        });



var result = (from du in data
              group du by new { du.Date, du.Name, du.PartnerId } 
              into gr
              select new
                     {
                      gr.Key.Date,
                      gr.Key.Name,
                      gr.Key.PartnerId,
                      Amount = gr.Count()
                     }
              ).OrderBy(d => d.Date);

1 个答案:

答案 0 :(得分:1)

尝试命名您的匿名类型属性

 var result = (from users in userService.GetListSimple()
                             .Where(d => d.RegisterTime >= new DateTime(2013, 01, 01) 
                                    && d.RegisterTime <= new DateTime(2013, 01, 10,23,59,59))
               group users by new {
                          Date = EntityFunctions.TruncateTime(users.RegisterTime), 
                          Name = users.partners.Name, 
                          PartnerId = users.partners.PartnerId } into gr
                              select new
                              {
                                  gr.Key.Date,
                                  gr.Key.Name,
                                  gr.Key.PartnerId,
                                  Ammount = gr.Count()
                              }
                ).OrderBy(d => d.Date);