Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 254
Tion Avatar asked May 26 '11 17:05

Tion


2 Answers

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

var source = myViewModel.MyListCollectionView.Cast<MyType>().Select(p=>p.MyProperty);
like image 188
Tion Avatar answered Oct 03 '22 09:10

Tion


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>();
like image 26
Jon Skeet Avatar answered Oct 03 '22 10:10

Jon Skeet