I add Custom type: Task
in to the list.
e.g.
Task t1 = new Task();
result.Add(t1);
Now i want to order 'result'
by the EndDate
property of Task
class.
I add Custom type: Task
in to the list.
e.g.
Task t1 = new Task();
result.Add(t1);
Now i want to order 'result'
by the EndDate
property of Task
class.
You should use List<Task>
instead of List<object>
then you can use OrderBy
Like this:
var sorted = result.OrderBy(c => c.EndDate);
Or:
var sorted = result.OrderByDescending(c => c.EndDate);
Or:
var sorted = result.OrderByDescending(c => c.EndDate).ToList();
What you need is a List<Task>
to be the type of result
. Right now you have List<object>
and you can't access EndDate
property without casting.
List<Task> result = new List<Task>();
Task t1 = new Task();
result.Add(t1);
and then for sorting you can use OrderBy
or OrderByDescending
var sortedQuery = result.OrderBy(r=> r.EndDate);
If you need a List<Task>
then append ToList();
to the query.
Also, you may change your class name to something different, as there is a Task
class available in .Net framework as well.
You can use LINQ by a typed List<>.
For example:
List<Task> result = new List<Task>();
result.Add(Task1);
result.Add(Task2);
return result.OrderBy(x => x.EndDate).ToList();