C# 从列表中查找特定类的对象<&燃气轮机;

C# 从列表中查找特定类的对象<&燃气轮机;,c#,generic-list,C#,Generic List,我有一个基类,比如叫做Fruits。然后我有几个这样的子类,比如香蕉:水果,苹果:水果,等等 然后我有一个不同类型的对象列表,香蕉,苹果,等等。看起来是这样的: List<Fruits> f = new List<Fruits>{new Banana(), new Banana(), new Apple(), new Banana()}; 但是我不知道如何使它适用于任何类。这样的方法已经存在于框架中-: List香蕉=f.OfType().ToList(); 尽管您应该

我有一个基类,比如叫做
Fruits
。然后我有几个这样的子类,比如
香蕉:水果
苹果:水果
,等等

然后我有一个不同类型的对象列表,香蕉,苹果,等等。看起来是这样的:

List<Fruits> f = new List<Fruits>{new Banana(), new Banana(), new Apple(), new Banana()};

但是我不知道如何使它适用于任何类。

这样的方法已经存在于框架中-:

List香蕉=f.OfType().ToList();

尽管您应该使用其他答案中提到的类型为的
,但作为一种学习经验,以下是我将如何编写您的方法:

public IEnumerable<Banana> GimmeBananas(List<Fruit> f)
{
     if (f == null) yield break; //depending on what business logic you're looking for.
     foreach(Fruit fr in f)
     {
         if (fr is Banana) yield return fr;
     }
}
public IEnumerable gimme(列表f)
{
if(f==null)产生break;//取决于您要寻找的业务逻辑。
foreach(水果果肉)
{
如果(fr为香蕉)收益率返回fr;
}
}

您可以使用LINQ OfType操作符从集合中仅选择具有匹配类型的元素

class Fruit { }
class Banana : Fruit{}
class Apple : Fruit{}

static void Main(string[] args)
{
    var fruits = new List<Fruit> { new Banana(),  new Banana(), new Apple(), new Fruit() };
    int bananas = fruits.OfType<Banana>().Count(); // 2
    int apples = fruits.OfType<Apple>().Count(); // 1
    int fruitc = fruits.OfType<Fruit>().Count(); // 4
    int exactFruits = GetOfExactType<Fruit,Fruit>(fruits).Count();  // 1
}


static IEnumerable<V> GetOfExactType<T, V>(IEnumerable<T> coll)
{
    foreach (var x in coll)
    {
        if (x.GetType().TypeHandle.Value == typeof(V).TypeHandle.Value)
        {
            yield return (V)(object)x;
        }
    }
}
类水果{}
香蕉类:水果{}
苹果类:水果{}
静态void Main(字符串[]参数)
{
var fruits=新列表{new Banana(),new Banana(),new Apple(),new Fruit()};
int bananas=fruits.OfType().Count();//2
int apples=fruits.OfType().Count();//1
int FROUTC=FROUTS.OfType().Count();//4
int exactFruits=GetOfExactType(fruits).Count();//1
}
静态IEnumerable GetOfExactType(IEnumerable coll)
{
foreach(coll中的变量x)
{
if(x.GetType().TypeHandle.Value==typeof(V.TypeHandle.Value)
{
收益率收益率(V)(对象)x;
}
}
}

此运算符仅在具有平坦派生层次结构时才起作用。如果您有例如苹果作为从水果派生的基类,并且您有专门的苹果,如GoldenDelicius、RedDelicus。。。你想要计算所有苹果类型的苹果,但不是派生的苹果,你不能用OfType操作符。为此,您仍然需要一个额外的函数。在本例中,它被称为GetOfExactType。

您需要一个通用方法,如:

    public List<T> GimmeAnyOfType<T>(List<Fruit> f)
    {
        return f.OfType<T>().ToList();
    } 
公共列表GimmeAnyOfType(列表f)
{
返回f.OfType().ToList();
} 

太棒了!这比我的目标要简单得多(也更短)。+1-foreach往往比linq快(取决于查询),而且这使开发人员有机会执行业务逻辑而不必重复两次。您可能希望用T替换Banana并添加到方法签名中。使其具有通用性,这正是OP所寻求的。
class Fruit { }
class Banana : Fruit{}
class Apple : Fruit{}

static void Main(string[] args)
{
    var fruits = new List<Fruit> { new Banana(),  new Banana(), new Apple(), new Fruit() };
    int bananas = fruits.OfType<Banana>().Count(); // 2
    int apples = fruits.OfType<Apple>().Count(); // 1
    int fruitc = fruits.OfType<Fruit>().Count(); // 4
    int exactFruits = GetOfExactType<Fruit,Fruit>(fruits).Count();  // 1
}


static IEnumerable<V> GetOfExactType<T, V>(IEnumerable<T> coll)
{
    foreach (var x in coll)
    {
        if (x.GetType().TypeHandle.Value == typeof(V).TypeHandle.Value)
        {
            yield return (V)(object)x;
        }
    }
}
    public List<T> GimmeAnyOfType<T>(List<Fruit> f)
    {
        return f.OfType<T>().ToList();
    }