Andrey Sanches

é MVP em ASP/ASP.NET e escreve sobre desenvolvimento WEB com tecnologias Microsoft

How to find objects in Generics with List.Find() method

I've been looking for help on how to find objects in Generics with List.Find() method .... and ... take a look what I have found.
In the follow example, I created a simple class:

public class Person
{
       private int _id;
       private string _name;

       public int ID {  get{ return _id;} set{ _id = value;}}
       public int Name {  get{ return _name;} set{ _name= value;}}

       public Person(int id, string name)
       {
             _id = id;
             _name = name;
       }
}

In the example, there's a simple class with two private attributes. Now we're going to create a typed List of this object and take advantage of the Find() method

public void CreateAndSearchList()
{
      //create and fill the collection
      List<Person> myList = new List<Person>();
      myList.Add(new Person(1, "AndreySanches"));
      myList.Add(new Person(2, "AlexandreTarifa"));
      myList.Add(new Person(3, "EmersonFacunte"));

     //find a specific object
     Person myLocatedObject = myList.Find(delegate(Person p) {return p.ID == 1; });
}

This is the fastest way to find objects using Generics.

Enjoy it !!!!








}

 

Posted: Wed, Apr 25 2007 11:59 by andrey | with 22 comment(s)
Filed under:

Comments

Suleyman V said:

Thank you very much.

Your example is very useful and easy to understand :)

# April 27, 2007 7:45 AM

Israel Aece said:

In C# 3.0 is much more simple :)

Person result =

    myList.Find(p => p.Name == "Israel");

# May 28, 2007 12:47 PM

Derek said:

What is this syntax in VB.net?

//find a specific object

Person myLocatedObject = myList.Find(delegate(Person p) {return p.ID == 1; });

# June 17, 2007 7:55 PM

Steve Kumbsky said:

Thanks for this tip! I was struggling with a way to do a Find using an outside value on an object property in the List. This is the answer.

# June 26, 2007 5:31 PM

Vic Moran said:

I'm sorta using something like this (except I defined a separate delegate method and passed the pointer to Find).  

The problem is, in order to get at the ID, you have to explicitly instantiate a specific object type (Person) in order to use this method; defeats the purpose uf using generics, doesn't it! And what's with the delegate, anyway?  Give me a way to specify a key or let "Find" search without the need for a delegate (very hokey).  I know it's hard to code a generic "Find" because the generic list can take any type, but use Reflection, something; once the list is typed, "Find" should know how to handle it.  There are other ways to check for reference type equality.

The other issue with List<T> is that "IndexOf" doesn't work with objects (assuming all ref types) either.

I know they improved performance (mostly) and added type safety over ArrayList, but we need a better generic list construct.

# October 26, 2007 2:10 PM

Ramesh said:

Thank you very much. I have been searching for this and atlast I found here.

# December 13, 2007 6:50 PM

Dennis Decoene said:

In your example, you are search for ID 1. This 1 is a fixed value. How would make it so the search parameter is dynamic?

# December 19, 2007 4:51 AM

Zack Stauber said:

This is a great example.  Thanks!

# December 24, 2007 12:53 PM

Bas said:

Just wanted to say that this a very good example and as I am using Visual Studio 2008, the code by Israel works perfectly for me. Cheers!

# January 4, 2008 9:05 AM

renato said:

VERY GOOD EXAMPLE!! CHEERS!

# July 25, 2009 4:06 PM

Vivek Chauhan said:

what i do when i need to find the record corresponding two value like ID and name both in above example.

# September 16, 2009 9:56 AM

andrey said:

Hi Vivek,

Take a look at the following C# code :

Person myLocatedObject = myList.Find(delegate(Person p) {return p.ID == 1 && p.Name == "AndreySanches"; });

Let me know if you have any other questions

# September 16, 2009 10:55 AM

Ranjit said:

While this rapid fire code is nice, it leaves more to be desired. One, rarely would a dynamic code search for id = 1 and name as Andrey. In real situations when the name is not known at design time, how do you search for a person with a name that is known at run time? Secondly, I wish someone pointed out the code in vb.net.

# September 17, 2009 1:42 PM

andrey said:

Hi Ranjit,

Actually this code is just an example. In the real situation you would need to replace number "1" and string "AndreySanches" for a local variable.

Eg.

int id = Convert.ToInt32(txtCustomerId.Text);

string name = txtCustomerName.Text;

Person myLocatedObject = myList.Find(delegate(Person p) {return p.ID == id && p.Name == name; });

Thanks for your comments

# September 17, 2009 2:08 PM

Khoa Do said:

List<T>.Find() is not so cryptic after reading your post.

Thanks for sharing your expertise.

# September 18, 2009 4:32 AM

Anders said:

Thanks for this :) Helpful in order to understand delegates.

# October 21, 2009 7:42 AM

Shilpi said:

Thanks a lotttttttttttttttttttttttt

# November 4, 2009 12:38 AM

kiran said:

Can u tell me it is work for datetime also.

i ahve the same class above and contains one more property as  

private DateTime _joinDate;

 public DateTime  JoinDate

       {

           set { _joinDate = value; }

           get { return _joinDate; }

       }

now i want to find employee who joined recently??

can you show me how this works?

thanks in advance

Kiran .

# November 9, 2009 5:17 AM

Hal said:

Well done!

# November 9, 2009 9:17 AM

andrey said:

Hi Kiran,

You can use the "JoinDate" property to search the objects.

e.g.

Person myLocatedObject = myList.Find(delegate(Person p) {return p.ID == id && p.JoinDate == #yourdateobject#; });

Let me know if you need anything else.

Thanks

# November 9, 2009 9:28 AM

michael said:

GREAT SIMPLE TUTORIAL

# December 9, 2009 8:52 AM

Karthik said:

Thanks Much!

This was very useful ... well explained!

Easier than the MSDN website explanation.

# December 17, 2009 2:05 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)