使用 linq 从 IEnumerable 中排除类型

发布于 2025-01-01 05:07:39 字数 841 浏览 4 评论 0原文

如何使用 linq-to-objects 根据对象的派生类型过滤对象?

我正在寻找性能最佳的解决方案。

使用的类:

abstract class Animal { }
class Dog : Animal { }
class Cat : Animal { }
class Duck : Animal { }
class MadDuck : Duck { }

我知道三种方法:使用 is 关键字、使用 Except 方法和使用 OfType 方法。

List<Animal> animals = new List<Animal>
{
    new Cat(),
    new Dog(),
    new Duck(),
    new MadDuck(),
};

// Get all animals except ducks (and or their derived types)
var a = animals.Where(animal => (animal is Duck == false));
var b = animals.Except((IEnumerable<Animal>)animals.OfType<Duck>());

// Other suggestions
var c = animals.Where(animal => animal.GetType() != typeof(Duck))

// Accepted solution
var d = animals.Where(animal => !(animal is Duck));

How can I filter out objects based on their derived type with linq-to-objects?

I am looking for the solution with the best performance.

The classes used:

abstract class Animal { }
class Dog : Animal { }
class Cat : Animal { }
class Duck : Animal { }
class MadDuck : Duck { }

I know of three methods: Use the is keyword, use the Except method, and to use the OfType method.

List<Animal> animals = new List<Animal>
{
    new Cat(),
    new Dog(),
    new Duck(),
    new MadDuck(),
};

// Get all animals except ducks (and or their derived types)
var a = animals.Where(animal => (animal is Duck == false));
var b = animals.Except((IEnumerable<Animal>)animals.OfType<Duck>());

// Other suggestions
var c = animals.Where(animal => animal.GetType() != typeof(Duck))

// Accepted solution
var d = animals.Where(animal => !(animal is Duck));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一个人的夜不怕黑 2025-01-08 05:07:39

如果您还想排除 Duck 的子类,那么 is 是最好的。您可以将代码缩短为 .Where(animal => !(animal is Duck));

否则,sll 推荐的 GetType 是最好的

If you want to also exclude subclasses of Duck, then the is is best. You can shorten the code to just .Where(animal => !(animal is Duck));

Otherwise, sll's recommendation of GetType is best

怪我闹别瞎闹 2025-01-08 05:07:39
  • 使用 Except() 的解决方案相当繁重。
  • 请记住,解决方案 - 即使某些从Duck继承的SomeDuck类也会返回true

    类 SomeDuck :鸭子
    ...
    // 鸭子是鸭子 == true
    var duck = new SomeDuck();
    

其他解决方案可以是:

animals.Where(animal => animal.GetType() != typeof(Duck))
  • Solution using Except() is quite heavy.
  • Keep in mind that solution is - would return true even some SomeDuck class inherited from Duck

    class SomeDuck : Duck
    ...
    // duck is Duck == true
    var duck = new SomeDuck();
    

An other solution could be:

animals.Where(animal => animal.GetType() != typeof(Duck))
梦醒时光 2025-01-08 05:07:39

根据 OfType<>() 和检查类型之间的差异Where() 扩展 OfType 调用等效于您的选项 (a),尽管 is Duck==true,因此基于此我会说坚持选项 (a)。

According to Difference between OfType<>() and checking type in Where() extension the OfType call is equivalent to your option (a), albeit with is Duck==true, so based on that I would say stick to option (a).

假扮的天使 2025-01-08 05:07:39

如果您不希望返回 DuckDuck 的任何子类,则需要使用 IsAssignableFrom 方法:

animals.Where(animal => !animal.GetType().IsAssignableFrom(typeof(Duck)));

If you don't want Duck nor any subclass of Duck to be returned, you need to use the IsAssignableFrom method:

animals.Where(animal => !animal.GetType().IsAssignableFrom(typeof(Duck)));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文