Find Method on Generic List

F

Fresno Bob

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.
 
A

Anthony Jones

Fresno Bob said:
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.

You don't state your Language or version. In C# 3:-

Customer cust = list.Find(c => c.CustomerID == 1);

In C# 2 you need to bloat the lambda to a delegate:-

Customer cust = list.Find(delegate(Customer c) { return c.CustomerID ==
1;});

I don't do VB so but the principle I'm sure is the same.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top