Yeah, I agree. It's not that bad. Find, or Exist, or Contains basically carry out the same looping code anyway.
Doug's suggestion for another tracking collection like a dictionary may speed things up a little.
If you were using C#, you could use anonymous delegates to accomplish the same, but with less code. I realize the following is of no use to you because you write in
VB, but it's an interesting solution (IMO) nonetheless:
Code:
public static bool DemoMethod()
{
List<Category> myList = new List<Category>();
myList.Add(new Category() { ParentId = 10, Id = 1 });
myList.Add(new Category() { ParentId = 14, Id = 2 });
myList.Add(new Category() { ParentId = 12, Id = 3 });
int parentId = 14;
return myList.Exists(delegate(Category myCategory) { return myCategory.ParentId == parentId; });
}
The code uses C# 3 object initializers to quickly fill the list, but you can of course replace that with another way to fill the list (I just needed some sample data). Additionally, it uses a hardcoded value for the parentId, but it gets the job done. The anonymous delegate is like an inline method. You send it an instance of Category which you can use to compare its ParentId to the parentId variable in the method body.
Again, a useless reply in your situation, but it may help someone else who needs a C# version in the future.
Cheers, and sorry for the off-topic reply..... ;)
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of
ASP.NET 2.0 Instant Results and
Beginning Dreamweaver MX / MX 2004