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
  • 13,320
  • 37
  • 106
  • 184

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,486
  • 4
  • 43
  • 62
  • 18
    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
  • 24,648
  • 6
  • 68
  • 78
0

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

ken2k
  • 48,145
  • 10
  • 116
  • 176