views:

69

answers:

2
+1  Q: 

findall in List<T>

I can't find any FindAll method in my List, how can i select objects from the List that respond to a specific criteria, without using the old iterating method?

List<oPage> mylist = new List<oPage>();

my oPage class has a property called Title of type string.

I added a few items of oPage inside myList.

now i want to select all items inside mylist that have a title containing the word 'abc' and return all those items in a IEnumerable.

how is it possible?

Thanks for your help.

+5  A: 

If you're using .NET 3.5 or later, you can use LINQ to do just that

mylist.Where(p => p.Title.Contains("abc"));
Rup
+2  A: 

The FindAll method returns a List, but you can just cast the results to an IEnumerable<oPage>:

List<oPage> mylist = GetYourList();

IEnumerable<oPage> results = (IEnumerable<oPage>)myList.FindAll(
   delegate(oPage p)
   {
      return p.Title.Contains("abc");
   }
);
GenericTypeTea
Yeah, exactly. Though do please use a lambda expression rather than anonymous delegate for brevity, if you're using C# 3.0+.
Noldorin