用LINQ计算组中的指定列

用LINQ计算组中的指定列

我得到了以下查询,该查询执行得很好,除了它始终具有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 }

                                         });

最佳答案

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

关于c# - 用LINQ计算组中的指定列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10073607/

10-11 04:39