Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linq filtering by class type

I have a list contains 2 different types of objects which are polylines and texts. I want to create a new list of polylines only.

what I do is;

var list2 = list1.SelectMany(x=> x.Type == PolyLine)

Error:'PolyLine' is a type, which is not valid in the given context.

How do I filter those objects here?

like image 327
Meriç Kıranoğlu Avatar asked Mar 23 '26 19:03

Meriç Kıranoğlu


1 Answers

Simply use the OfType<T> extension:

var list2 = list1.OfType<PolyLine>().ToList();

This selects all elements in list1 that are of type PolyLine.

After ToList() the resulting type of list2 is List<PolyLine>.

like image 52
René Vogt Avatar answered Mar 26 '26 10:03

René Vogt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!