I am using EF Core version 2.2.6.
I would like to order by descending a list based on an inner list using IQueryable
.
I have a list of courses that each course have a list of course progresses, and I would like to order the course list based on the inner list course progresses' UpdatedDate
.
And I am building the LINQ query programmatically before executing it against the database.
The code is as follows
IQueryable<Course> query = _dbContext
.Courses
.AsNoTracking()
.Include(x => x.CourseProgresses)
if (page > 0 && pageSize > 0)
query = query.Skip((page - 1) * pageSize)
.Take(pageSize);
if (watched == false)
{
query = query.OrderByDescending(x => x.LastUpdatedAt);
}
else
{
// TODO: THIS IS THE PART I NEED TO FIX
query = query.OrderByDescending(x => x.CourseProgresses.OrderByDescending(y => y.UpdateDate));
}
var courses = await query.ToListAsync();
When executing the line with the query
query = query.OrderByDescending(x => x.CourseProgresses.OrderByDescending(y => y.UpdateDate));
and after on when trying to make the ToListAsync
I am getting the error as follows:
Failed to compare two elements in the array.
At least one object must implement IComparable.
I saw another post explaining how to do it using .Sort
, but the problem is that I would need to work with List
, instead of the IQueryable
, does anyone have a workaround for this problem?