How can I implement NotOfType in LINQ that has a nice calling syntax?

前端 未结 7 1197
轻奢々
轻奢々 2020-12-11 00:24

I\'m trying to come up with an implementation for NotOfType, which has a readable call syntax. NotOfType should be the complement to OfType&l

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-11 00:56

    I had a similar problem, and came across this question whilst looking for an answer.

    I instead settled for the following calling syntax:

    var fewerAnimals = animals.Except(animals.OfType());
    

    It has the disadvantage that it enumerates the collection twice (so cannot be used with an infinite series), but the advantage that no new helper function is required, and the meaning is clear.

    In my actual use case, I also ended up adding a .Where(...) after the .OfType() (giraffes also included unless they meet a particular exclusion condition that only makes sense for giraffes)

提交回复
热议问题