![](/img/trans.png)
[英]How to determine whether an object can be convert to meaningful string in C#
[英]C# how to determine, whether ArrayList contains object with certain attribute
我有一個自定義類的對象的ArrayList。 我想知道,如果ArrayList包含具有特定屬性的對象。 我不關心這個對象,即使有一些對象也是如此。 是的,我可以在foreach周期中執行此操作,但是我想知道是否有更優雅的方法可以執行此操作。
感謝您的建議。
好吧,首先我建議使用List<T>
而不是ArrayList
。 然后,LINQ to Objects變得非常簡單:
if (list.Any(x => x.HasFoo))
{
}
或不使用LINQ(但仍為List<T>
)
if (list.FindIndex(x => x.HasFoo) != -1)
{
}
如果您確實需要堅持使用非通用集合,但也可以使用LINQ to Objects,則可以使用:
if (arrayList.Cast<YourType>().Any(x => x.HasFoo))
{
}
使用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.
檢查該msdn示例: Linq示例
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.