Reverse OfType in Linq?

Possible duplicate:
How can I implement NotOfType <T> in LINQ that has good call syntax?

I have a collection where I want to iterate over all objects that are not of type Foo . Basically, I would like something opposite OfType<Foo>() . What would be a suitable way to do this?

So far I have used:

 var result = myList.Where(elem => !(elem is Foo)); 
+6
source share
2 answers

There is nothing wrong with the code.

If you want to reuse this code, you can ecnapsulate this in the extension method, as is done for OfType<Foo>()

+9
source

Maybe like

 var result = myList.Where(elem => (elem as Foo)==null) 

Hope this helps.

0
source

Source: https://habr.com/ru/post/911109/


All Articles