LINQ query finds subclass in an ObservableCollection of base class
I'm still getting my head around LINQ queries and the LINQ extension methods. 开发者_如何学运维
Say I have a base class BaseA
and subclass SubB
with a property IsAwesome
. I have an ObservableCollection<BaseA>
. Is there some neat way I can grab a collection of only SubB classes where IsAwesome is true?
Or better:
myCollection.OfType<SubB>().Where(x => x.IsAwsome);
You can do:
myCollection.Where(x => x is SubB && (x as SubB).IsAwesome);
精彩评论