开发者

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' 开发者_StackOverflowand return all those items in a IEnumerable.

how is it possible?

Thanks for your help.


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

mylist.Where(p => p.Title.Contains("abc"));


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");
   }
);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜