I've got a question on LINQ Expression. I need to replicate the following lambda:
var result = processRevisions
OrderByDescending(pr => pr.CreatedAt)
GroupBy(pr => pr.IdProcess)
Select(pr => pr.First());
which result is the latest revision created for every process, in LINQ expression.
I start with this code:
Expression<Func<ProcessRevision, DateTime>> orderProcessRevision = f => f.CreatedAt;
Expression<Func<ProcessRevision, long>> groupProcessRevision = f => f.IdProcess;
Expression<Func<ProcessRevision, ProcessRevision>> selectProcessRevision = f => f;
processRevisions = await processRevisionRepository.GetDatasByLastRevisionAsync(orderProcessRevision, groupProcessRevision, selectProcessRevision);
The repository is as follows:
public async Task<IEnumerable<T>> GetDatasByLastRevisionAsync(
Expression<Func<T, DateTime>> order,
Expression<Func<T, long>> group,
Expression<Func<T, T>> select)
{
IQueryable<T> query = dbContext.Set<T>();
query = query.OrderByDescending(order);
query = query.GroupBy(group);
query = query.Select(select);
return await query.ToListAsync();
}
The only part of the query which works is the orderbydescending
. Then, the groupby
yields an error (implicit convert IGrouping
to IQueryable
) and also the select first item of each group goes in error.
How to apply the groupby
(igrouping
) to IQueryable
and for every group take the first object?
Thanks for help.