Fresno Bob wrote:
I have a generic collection of objects and I would like to find the object
by one of it's properties e.g. I would like something with the functionality
of something like list.find(Customer.CustomerID = 1). Is there an easy way
to do this. The stuff with predicates and findall is a little confusing.
I hope this cures some of your confusion:
The FindAll method uses a delegate, so you can just make a method and
use with the FindAll method:
public static bool FindCustomerOne(Customer c) {
return c.CustomerID == 1;
}
List<Customerresult = myCustomerList.FindAll(FindCustomerOne);
The FindAll method will just loop through all the items in the list and
call the FindCustomerOne method for each one to determine which one to
return in the result.
You can also do the same with an anonymous method:
List<Customerresult = myCustomerList.FindAll(
delegate(Customer c){ return c.CustomerID == 1; }
);
If you are using C# 3, you can also use a lambda expression to do the same:
List<Customerresult = myCustomerList.FindAll(c =c.CustomerID == 1);
--
Göran Andersson
_____
http://www.guffa.com