Search code examples
c#wpflinqmvvmlistcollectionview

How do I write a linq query against a ListCollectionView?


none of these seem to do the trick:

var source = myViewModel.MyListCollectionView.Select(x => x as MyType);
var source = myViewModel.MyListCollectionView.Select<object, MyType>(x => x as MyType);
var source = myViewModel.MyListCollectionView.SourceCollection.Select<object, MyType>(x => x as MyType);

Solution

  • ListCollectionView only implements the non-generic IEnumerable interface. I suspect you want:

    var source = myViewModel.MyListCollectionView.Cast<MyType>();
    

    or (if some values won't be of MyType, and that's okay):

    var source = myViewModel.MyListCollectionView.OfType<MyType>();