Counting specific columns in a group with LINQ
I got the following request, which works well, except that it always has ResubCount = 1 and not a value for each UserId.
Now:
ResolvedDate: Date
ResubCount = 1
UserId = UserId
Now the query returns (say) ten results from the database.
I want it to be like this:
ResolvedDate: Date
ResubCount = 10
UserId = UserId
This is my request:
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 }
});
+3
source to share
1 answer
I believe the temporary part of the DateTime can affect the grouping:
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
});
Edit: I had
a.ResolvedDate.Date
, however I don't believe this is supported.
+1
source to share