2012-04-09 35 views
3

我得到了以下查詢,該查詢執行得很好,除了它始終具有ResubCount = 1,而不是每個UserId的值。使用LINQ計算組中的指定列

現在:

ResolvedDate: Date 

ResubCount = 1 

UserId = UserId 

查詢現在返回(比方說)從數據庫十個結果。

我希望它是這樣的:

ResolvedDate: Date 

ResubCount = 10 

UserId = UserId 

這是我的查詢:

var result = (from a in _dataContext.Activities 
          where a.IsResolved && a.ResolvedDate != null 
          group a by new { a.ResolvedDate, a.UserId } 
           into agroup 
           select new 
             { 
              ResolvedDate = EntityFunctions.TruncateTime(agroup.Key.ResolvedDate), 
              ResubCount = agroup.Count(), 
              UserId = from item in agroup select new { item.UserId } 

             }); 

回答

1

相信的DateTime的時間部分可能會影響分組:

var result = (from a in _dataContext.Activities 
       where a.IsResolved && a.ResolvedDate != null 
       group a by new 
       { 
        ResolvedDate = EntityFunctions.TruncateTime(a.ResolvedDate), 
        UserId = a.UserId 
       } into agroup 
       select new 
       { 
        ResolvedDate = agroup.Key.ResolvedDate, 
        ResubCount = agroup.Count(), 
        UserId = agroup.Key.UserId 
       }); 


編輯:我有 a.ResolvedDate.Date,但是,我沒有相信是支持的。

+0

感謝您的回答,但是這給出了一個編譯錯誤:無效的匿名類型成員聲明。匿名類型成員必須聲明爲成員分配,簡單名稱或成員訪問權限。 - 你是對的,.Date不被支持,因爲它是一個可以爲空的日期時間和LINQ2SQL – SeToY 2012-04-09 13:10:24

+0

不是調用可以爲空的DateTime的'.Value'屬性的情況嗎? – everton 2012-04-09 13:18:54

+0

這會導致編譯錯誤:'AnonymousType#1'不包含'ResolvedDate'的定義,並且沒有找到接受'AnonymousType#1'類型的第一個參數的擴展方法'ResolvedDate'(你是否缺少using指令或程序集引用?) – SeToY 2012-04-09 13:19:12