2013-07-30 55 views
2

我需要返回特定用戶每天約會的最後30天,並檢查用戶是否每天至少進行8小時的約會。如何在LINQ中進行分組?

在SQL我可以做到這一點與這個命令:

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

之後,我得到的結果和驗證DurationInHours(double類型)。

使用LINQ可以做到嗎? 獲取上個月用戶預約的列表並日後驗證它

謝謝!

+1

是的!你試過什麼了? –

+1

[在LINQ中分組]的可能重複(http://stackoverflow.com/questions/7325278/group-by-in-linq) –

+1

@Brian他有多個鍵,但。 –

回答

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

UPDATE:

僅供參考您的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)}); 
+0

應該注意的是,OP提到'30天',而不是'1月',這是不同的,但它是相同的想法。 –

+0

它幾乎可以工作,但我的存儲庫默認返回一個列表。 groupby方法改變它的類型。我怎樣才能將「結果」再次轉換爲Note對象? – gog

+1

@ggui如果你把它變回一個筆記列表,你將失去分組和總和。您可以通過在總和後面添加Notes = g來將註釋添加到匿名結果中。這可以讓你選擇組成每個組的筆記。 – NinjaNye

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

需要得到一個明確的名稱(即TotalHours),或者你錯誤CS0746總和屬性:無效的匿名類型成員聲明。匿名類型成員必須聲明爲成員分配,簡單名稱或成員訪問權限。

+0

這是正確的matzoi,但我無法計算如何使用返回列表進行驗證併發送電子郵件給用戶。但是,謝謝 – gog

+2

如果您要問的是如何將結果轉換爲Note類型的元素,則只需在select子句中適當地構造這些對象即可。像這樣...從n個變種結果=在筆記 \t \t \t組用新的N- {n.IDUser,n.IDAppointment,n.AppointmentDate} \t \t \t成克 \t \t \t其中g.Key.AppointmentDate> lastMonth && g.Key.IDUser == selectedUserId \t \t \t \t \t \t \t選擇新注(){IDAppointment = g.Key.IDAppointment,ID用戶所= g.Key.IDUser,DurationInHours = g.Sum(N =>ñ 。DurationInHours)};' – matzoi