-2

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?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Tito
  • 722
  • 4
  • 26
  • 55
  • It doesn't, in the question you mentioned is showing only one element, I would need a list of elements (Course in my case) as a return value – Tito Dec 13 '21 at 10:31
  • Nope, it still doesn't I get an exception when I add the code you mentioned: query = query.Include(x => x.CourseProgresses.OrderByDescending(y => y.UpdateDate)); – Tito Dec 13 '21 at 10:42
  • Exception: The Include property lambda expression 'x => {from CourseProgress y in x.CourseProgresses orderby [y].UpdateDate desc select [y]}' is invalid. The expression should represent a property access: 't => t.MyProperty'. To target navigations declared on derived types, specify an explicitly typed lambda parameter of the target type, E.g. '(Derived d) => d.MyProperty'. – Tito Dec 13 '21 at 10:42
  • Could you include used EF version in the question? – Selvin Dec 13 '21 at 10:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/240094/discussion-between-tito-and-selvin). – Tito Dec 13 '21 at 12:29
  • update to EF Core 5.0 and answer should work – Selvin Dec 13 '21 at 17:24
  • this is not a proper answer – Tito Dec 14 '21 at 20:04
  • 1
    This is not a proper question .. we don't know what is relatioship between `Courses` and `CourseProgresses` – Selvin Dec 14 '21 at 23:29

1 Answers1

0

If you want to order by nested elements, you could do it like this:

query = query.OrderByDescending(x => 
    {
        x.CourseProgresses = x.CourseProgresses.OrderByDescending(y => y.UpdateDate));
        return x.Id;
    }
Sebastian Siemens
  • 2,302
  • 1
  • 17
  • 24
  • thanks for the answer, but it complains on the x.CourseProgresses.OrderByDescending(y => y.UpdateDate)) piece: Can't convert from IOrderedEnumerable to ICollection. I have tried the something similar before, but I was ending up with this same error – Tito Dec 13 '21 at 10:27
  • Maybe this question will help you: https://stackoverflow.com/questions/18853573/sort-a-list-and-all-its-nested-objects-using-linq – Sebastian Siemens Dec 13 '21 at 10:34
  • Hello Sebastian, thanks a lot for the recommendation, but I am getting an error with casting when checking this answer you pointed me out. PS: Could you help me reopen this question? Someone is downvoting everything and is trying to close it, unfortunately. – Tito Dec 13 '21 at 14:49