I have an ArrayList of objects of my custom class. I would like to know, if ArrayList contains object with certain attribute. I do not care about the object, just if there is some. Yes, I could do this with foreach cycle, but I was wondering if there was more elegant way to do so.
Thanks for suggestions.
Well, to start with I'd suggest using
List<T>
instead ofArrayList
. Then LINQ to Objects makes it really easy:Or without LINQ (but still
List<T>
)If you really need to stick with a non-generic collection but have LINQ to Objects available too, you can use:
use Linq:
check that msdn example: Linq example