Gets exceptions ""at least one object must implement icomparable"" only when apply method ToList() to IQueryable<>

17,827

Solution 1

The problem is here:

.OrderBy(s => s)

Compiler doesn't know how to compare your values and perform the ordering.Your type must implement IComparable (though it's anonymous type) or you can make your sort by some property:

.OrderBy(s => s.ID)

Solution 2

You are calling OrderBy but your expression s => s is just yielding the anonymous type created in the select new above. Anonymous types are not comparable, so you can't order by them. Maybe you mean .OrderBy(s => s.Name)?

Solution 3

When you are calling OrderBy with your expression like c => c.someParameter and if your some parameter is reference parameter of other table then also you will get same error like e.g.

var sortedOrders = allOrders.OrderBy(s =>s.Branch);

In above example all orders is collection of orders and I want to sort then on branch. But branch is reference column of Branch Table. So I was getting error.

Hence if you are ordering by using reference column then you should do in following way.

var sortedOrders = allOrders.OrderBy(s =>s.Branch.Id)

or any parameter form branch column like branch name etc.

Share:
17,827
Ark
Author by

Ark

Updated on July 21, 2022

Comments

  • Ark
    Ark almost 2 years

    When i don't use ToList() method to query firsQue everything is "Ok!" but i need execute firsQue immediately! Then i added to the end ToList() method and got exception in second query secQue "at least one object must implement IComparable".


    I dont understand that caused it, grouping performs by g.Action.ActionType.Name... It's string, string type implements IComparer interface

    static void Main(string[] args)
    {
        var firsQue  = GetAll()
            .SelectMany(s => s.Goals
                .Where(s2 => s2.ID == 2)
                 .SelectMany(f => f.Actions
                     .SelectMany(h => h.GoalProgresses))).ToList();
    
    
        var secQue = (from g in firsQue  
                            group g by g.Action.ActionType.Name into a
                            select new
                            {
                                Name = a.Key,
                                Duration = a.Sum(s => s.Action.Duration),
                                Done = a.Sum(s => s.DurationComplete),
                                RemainsToDo = (a.Sum(s => s.Action.Duration) - a.Sum(s => s.DurationComplete))
                            })
                             .OrderBy(s => s)
                             .ToList();
    
        System.Console.ReadLine();
    }
    
    static IQueryable<Patient> GetAll()
    {
        return db.Patients;
    }
    

    }

  • Ark
    Ark over 10 years
    Yes, it helped, and it logical. But that if i dont need to change order and need to use Skip(), Take() extention methods ? And the main question still exist, why it query work with .OrderBy(s => s) when i dont use ToList() at first query firsQue