I'm trying to do the retrieve the last record in each group like it does here
https://stackoverflow.com/a/20770779/4789608
but in Entity Framework Core. Based on what's on the link, I can get the SQL to work to bring back the correct data using
select *
from [Location]
where [LocationModelId] in (select max([LocationModelId])
from [Location]
group by [UserModelId])
or
select m1.*
from [Location] m1
left outer join [Location] m2 on (m1.[LocationModelId]< m2.[LocationModelId]
and m1.[UserModelId] = m2.[UserModelId])
This is the closest I've gotten based on that link
locationDetails = _context.Location
.GroupBy(p => p.UserModelId)
.Select(p => p.FirstOrDefault(w => w.UserModelId == p.Max(m => m.UserModelId)))
.OrderBy(p => p.DateCreated)
.ToList();
which returns this error message so, it's definitely not working.
The LINQ expression 'GroupByShaperExpression:\r\nKeySelector: l.UserModelId, \r\nElementSelector:EntityShaperExpression: \r\n EntityType: LocationModel\r\n ValueBufferExpression: \r\n ProjectionBindingExpression: EmptyProjectionMember\r\n IsNullable: False\r\n\r\n .FirstOrDefault(w => w.UserModelId == GroupByShaperExpression:\r\n KeySelector: l.UserModelId, \r\n ElementSelector:EntityShaperExpression: \r\n EntityType: LocationModel\r\n ValueBufferExpression: \r\n ProjectionBindingExpression: EmptyProjectionMember\r\n IsNullable: False\r\n\r\n .Max(m => m.UserModelId))' 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 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'