使用LINQ计算Group中的指定列

时间:2012-04-09 13:03:10

标签: c# sql linq count

我得到了以下查询,该查询表现良好,但它始终具有ResubCount = 1,而不是每个UserId的值。

现在:

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 个答案:

答案 0 :(得分:1)

我相信DateTime的Time部分可能会影响分组:

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但是,我认为不支持。