I have a group data in my database like that:
Code ModificationDate
--------------------------
A 2020/01/02
A 2020/01/01
B 2020/01/03
B 2020/01/01
C 2020/01/04
C 2020/01/01
And I want to get the value with the most recent ModificationDate from each group of codes.
I tried some linq queries, but I always get the same error
Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable'
To workaround it, I wrote this code:
var query = _context.Customers.FromSqlRaw(@"
SELECT t.* FROM(
SELECT[Code], MAX([ModificationDate]) as [ModificationDate]
FROM[Customers]
GROUP BY[Code]
) c
INNER JOIN[Customers] t
ON t.[Code] = c.[Code] AND t.[ModificationDate] = c.[ModificationDate]");
It works fine, but I want to translate it to a linq query that doesn't trigger the mentioned error.
I don't want to use AsEnumerable or ToList because I have a lot of data and load it into memory is going to be slow.
.AsEnumerable()does NOT load the whole data into memory!