How to search object in Generics?

Having a large list of objects and working with the list. When you want to find an object from that particular list is too cumbersome.

Mostly the newbie programers do this kind of mistakes and search the object with running a for/foreach loop. Which can not be accepted as it gives a heavy load to the server… accessing each object and checking each of them to search a suitable one.

This is the fastest, easiest and most reliable way to find objects using Generics. Lets take an example, we have a class Solution and have Generic List<> of the same:

Solution Class:

public class Solution
{
    public string Name { get; set; }
    public Guid Id { get; set; }
    public string UniqueName { get; set; }
    public Guid PublisherId { get; set; }
}

And to find the object, here is the code:

Solution result = this.SolutionList.Find(
    delegate(Solution s)
    {
        return s.Id == (Guid)this.SolutionsComboBox.SelectedValue;
    });

Check with result object if it is null, you do not have that object in your list.

Happy Programming!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s