C# how to determine, whether ArrayList contains object with certain attribute

情到浓时终转凉″ 提交于 2019-12-06 06:38:15

Well, to start with I'd suggest using List<T> instead of ArrayList. Then LINQ to Objects makes it really easy:

if (list.Any(x => x.HasFoo))
{
}

Or without LINQ (but still List<T>)

if (list.FindIndex(x => x.HasFoo) != -1)
{
}

If you really need to stick with a non-generic collection but have LINQ to Objects available too, you can use:

if (arrayList.Cast<YourType>().Any(x => x.HasFoo))
{
}

use Linq:

var query = from o in yourarray select o where o.atribute==ValueIWant;


`query.Count()` will return the number of objects that fit the condition.

check that msdn example: Linq example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!