LINQ result to ObservableCollection

By samjayander thiagarajan

How to select the result of a ObservableCollection from Linq back to Generic ObservableCollection instead of Generic List.

Consider you are using a LINQ over a ObservableCollection<Entity> and you want to get the Result Items as a list.

using .ToList(), you will end up in a List<Enity> and not ObservableCollection<Entity>

To solve this, you can have a Extension method like this and just use .ToObservableCollection<Entity>.

public static class ExtensionMethods
{
public static ObservableCollection<T> ToObservableList<T>(this IEnumerable<T> data)
{
ObservableCollection<T> dataToReturn = new ObservableCollection<T>();

foreach (T t in data)
dataToReturn.Add(t);

return dataToReturn;
}
}

A bit Handy. Thats it!!!

LINQ result to ObservableCollection  (3677 Views)