I am developing an ASP.NET Core 3.1 project. I want to group Services in Tickets based on the average of the difference between CreatedTime and ModifiedTime of Tickets.
Basically, I want to know the average minutes it takes for a ticket, of every type of service, to close.
Something like this:
==========================
Name | Avg. Mins
==========================
Service 1 | 10 Mins
Service 2 | 12 Mins
Service 3 | 20 Mins
==========================
Here is what I have tried:
var d = _dbcontext.Tickets.Include(m => m.Services)
.Where(m => m.ServiceId != null)
.GroupBy(t => new { Name = t.Services.Name })
.Select(m => new { x = m.Key.Name , y = m.Average(p => (p.CreatedDate - p.ModifiedDate).Minutes) })
.ToList();
But I am getting this error:
InvalidOperationException: The LINQ expression '((EntityShaperExpression: EntityType: Tickets ValueBufferExpression: (ProjectionBindingExpression: EmptyProjectionMember) IsNullable: False ).CreatedDate - (EntityShaperExpression: EntityType: Tickets ValueBufferExpression: (ProjectionBindingExpression: EmptyProjectionMember) IsNullable: False ).ModifiedDate).Minutes' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().
I tried AsEnumerable()
after GroupBy
but the GroupBy
cannot execute on client-side it seems. Need a good way to solve it.