The Self-Taught Programmer

Recipes from real-world experience with asp.net.

How to Find the Index of an Object in a List

leave a comment »

The Situation

You are trying to find out if an object exists in a List object.

A Solution

Use Linq. The example below looks for a User object in a List<User> by searching for the UserName:

private int FindObjectInList(List<User> list, string uName)
{
    return list.FindIndex(s => s.UserName == uName);
}
Advertisements

Written by Jim McMullen

February 1, 2013 at 10:23 pm

Posted in How-To

Tagged with ,

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

%d bloggers like this: