7

Possible Duplicate:
How can I implement NotOfType<T> in LINQ that has a nice calling syntax?

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

So far I've used:

var result = myList.Where(elem => !(elem is Foo));
Community
  • 1
  • 1
John-Philip
  • 617
  • 9
  • 20

2 Answers2

9

Nothing wrong with your code.

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

Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
0

Could be like this

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

Hope this helps.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • I think this is equivalent to the original. – Balazs Tihanyi Mar 19 '12 at 15:46
  • How is this better than `!(elem is Foo)`? – Daniel Hilgarth Mar 19 '12 at 15:47
  • @DanielHilgarth: it's a different and from performance point of view AS is a faster then IS. But from functional point of view, there is no difference. I just offer a different choice. – Tigran Mar 19 '12 at 15:49
  • 1
    @Tigran: Do you have any sources that verify that `as` is faster than `is`? I can't quite believe it. – Daniel Hilgarth Mar 19 '12 at 15:50
  • @DanielHilgarth: absolutely yes. Need to search for them, but I made my test too (years ago) and infact it was faster. – Tigran Mar 19 '12 at 15:53
  • @Tigran: Would you be so kind as to search for them? I am curious as to why this would be the case. – Daniel Hilgarth Mar 19 '12 at 15:57
  • @DanielHilgarth: unfortunately not able to find an article (if not mistake was something from CodeProject), but you can have look on [this](http://stackoverflow.com/questions/686412/c-sharp-is-operator-performance) and also make you own test. It's not a bug but difference between IS and AS operator functionality. – Tigran Mar 19 '12 at 15:59
  • @DanielHilgarth: [this](http://www.techgalaxy.net/Docs/Dev/5ways.htm) one too – Tigran Mar 19 '12 at 16:00
  • @DanielHilgarth: [this](http://blogs.msdn.com/b/csharpfaq/archive/2004/03/12/what-s-the-difference-between-cast-syntax-and-using-the-code-as-code-operator.aspx) one again... and so on. It'd easier to you to write a simple code with 10.000 (say) iteration and check result. – Tigran Mar 19 '12 at 16:02
  • The links don't proof your point. The second and third one compare a cast to the `as` operator, the accepted answer in the first one says `is` + cast is slower than `as`. The last link even seems to indicate that `as` and `is` are using the same IL instruction `isinst` and thus should be equivalent. – Daniel Hilgarth Mar 19 '12 at 16:07
  • And about the performance test: I don't trust these, because most of the time those tests are flawed, because the presumptions are wrong, they are only testing a specific case etc. – Daniel Hilgarth Mar 19 '12 at 16:09
  • @DanielHilgarth: well I should write a code for this, but have no time now. If you want a prove you should wait for it or do it by yourself. – Tigran Mar 19 '12 at 16:10
  • @Tigran I just measured it and I see no difference between `is` and `as` operators in speed. However, the `as` is preferred if you need to check the type as well as cast the object. – Balazs Tihanyi Mar 19 '12 at 16:11
  • @Tigran: As I said, those test codes are no proof. – Daniel Hilgarth Mar 19 '12 at 16:11
  • @BalazsTihanyi: hm... I repeat, I definitely checked it, but years ago. Would check it again soon on latest .NET versions. – Tigran Mar 19 '12 at 16:12
  • @DanielHilgarth: I mean the code writtent by yourself or myself not picked form inernet. – Tigran Mar 19 '12 at 16:13
  • 1
    @DanielHilgarth: hm.. you're right. I just run the code and it doesn't make a significant difference, and also in IL there is the same instruction call 'isinst' for both of them. Well, I was mistaken on performance benefit actually. Would be interesting to know if something changed in latest versions of .NET in regard of it, cause I checked it against earliest versions (1.0 or 2.0). – Tigran Mar 19 '12 at 17:01
  • @Tigran: That's why I was asking. There didn't seem to be a logical reason for this performance difference. And with the performance test code, you only tested one specific version of .NET, i.e. you tested implementation details that can change as your test now shows. – Daniel Hilgarth Mar 19 '12 at 17:07
  • @DanielHilgarth: I always (where it's possible) use "as", by the way. To me it seems just clearer. Mainly if the only thing that I would like to do is to check if specified object of certain type, but also in other cases too. – Tigran Mar 19 '12 at 17:09
  • 2
    @Tigran: If you only want to check whether an object is of a certain type - *without using it as that type later* - `is` is actually cleaner, because it communicates your intent more clearly and is the keyword exactly for this use case. – Daniel Hilgarth Mar 19 '12 at 17:10