Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
}

}

like image 871
Ark Avatar asked Jan 08 '14 20:01

Ark


2 Answers

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)
like image 55
Selman Genç Avatar answered Oct 15 '22 09:10

Selman Genç


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

like image 34
Michael Gunter Avatar answered Oct 15 '22 10:10

Michael Gunter