Tion
Tion

Reputation: 1490

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);

Upvotes: 13

Views: 6327

Answers (4)

Tion
Tion

Reputation: 1490

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

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

Upvotes: 2

Nathan Anderson
Nathan Anderson

Reputation: 6878

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

Upvotes: 2

Albin Sunnanbo
Albin Sunnanbo

Reputation: 47058

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

Upvotes: 7

Jon Skeet
Jon Skeet

Reputation: 1502406

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>();

Upvotes: 27

Related Questions