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

.net c# entity-framework linq


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)


static IQueryable<Patient> GetAll()
    return db.Patients;


1/8/2014 8:04:37 PM

Accepted Answer

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)
1/8/2014 8:08:57 PM

Popular Answer

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)?

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow