How to Find the Index of an Object in a List

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);
}

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 )

Connecting to %s