How do I write a linq query against a ListCollectionView?
Asked Answered
O

4

13

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);
Odelle answered 26/5, 2011 at 17:26 Comment(0)
D
27

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>();
Dragging answered 26/5, 2011 at 17:30 Comment(1)
Minor note: OfType will also remove null items.Sturrock
R
7
var source = myViewModel.MyListCollectionView.OfType<MyType>();
Retardant answered 26/5, 2011 at 17:30 Comment(0)
T
2

The InternalList property is of type IList so you would be able to write a linq query against that.

Towpath answered 26/5, 2011 at 17:30 Comment(2)
actually var source = myViewModel.MyListCollectionView.InternalList.Select(x=>x.MyProperty);doesn't workOdelle
This is the non generic IList so you would still have to use Cast<>. Not to mention that this is a protected property and you couldn't access it unless you were extending ListCollectionViewOdelle
O
2

ahhhh found it. you have to use Cast<> first!

var source = myViewModel.MyListCollectionView.Cast<MyType>().Select(p=>p.MyProperty);
Odelle answered 26/5, 2011 at 17:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.