Reputation: 1383
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;
}
}
Upvotes: 9
Views: 18047
Reputation: 471
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.
Upvotes: 1
Reputation: 101681
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)
Upvotes: 26
Reputation: 12811
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)
?
Upvotes: 4