7

I've tried:

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

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

Jason94
  • 12,922
  • 36
  • 102
  • 179

3 Answers3

19

You just need to create a new instance of it.

Persons = new ObservableCollection<Person>(from i in Persons orderby i.Age select i);
PhonicUK
  • 13,040
  • 4
  • 39
  • 62
  • 17
    This may break any binding that's using the `ObservableCollection`. – David S. May 15 '13 at 10:13
  • 1
    @DavidS if the class containing it implements `IPropertyNotifyChanged` and the property for the collection is set up properly then it shouldn't be an issue. – PhonicUK May 15 '13 at 10:16
  • it doesn't break anything, you have only to use some class for notify the update of the data – Piero Alberto Jun 23 '15 at 14:00
  • There really isn't any point to using an `ObservableCollection` if you're going to replace the whole thing each time. – geniusburger Apr 09 '17 at 03:36
1

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

Arran
  • 23,790
  • 6
  • 65
  • 76
0

You might want to simply create a new ObservableCollection from the sorted enumerable.

ken2k
  • 46,953
  • 10
  • 113
  • 166