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); }
Advertisements
Written by Jim McMullen
February 1, 2013 at 10:23 pm
Posted in How-To
Tagged with Generic List, List
Leave a Reply