2014-12-02 117 views
0

我想獲取一些數據並按日期對它進行分組。 下一個表達式不適合我,EntityFunctions.TruncateTime(users.RegisterTime)inavlid類型的成員聲明。Linq到MySql組按日期

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

嘗試命名匿名類型的屬性

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);