Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I sort ObservableCollection? [duplicate]

Tags:

c#

I've tried:

Persons = from i in Persons orderby i.Age select i;

But I cant convert Linqs System.Linq.IOrderedEnumerable to ObservableCollection<Person>.

like image 1000
Jason94 Avatar asked May 15 '13 10:05

Jason94


2 Answers

You just need to create a new instance of it.

Persons = new ObservableCollection<Person>(from i in Persons orderby i.Age select i);
like image 125
PhonicUK Avatar answered Nov 11 '22 05:11

PhonicUK


An ObservableCollection can take in an IEnumerable<T> (i.e, in this instance, your IOrderedEnumerable) from it's constructor:

http://msdn.microsoft.com/en-us/library/cc679169.aspx

like image 1
Arran Avatar answered Nov 11 '22 05:11

Arran