tags:

views:

1396

answers:

4

All the examples I see of using the IndexOf() method in List are of basic string types. What I want to know is how to return the index of a list type that is an object, based on one of the object variables.

List<Employee> employeeList = new List<Employee>();
employeeList.Add(new Employee("First","Last",45.00));

I want to find the index where employeeList.LastName == "Something"

+8  A: 
int index = employeeList.FindIndex(employee => employee.LastName.Equals(somename, StringComparison.Ordinal));

Edit: Without lambdas for C# 2.0 (the original doesn't use LINQ or any .NET 3+ features, just the lambda syntax in C# 3.0):

int index = employeeList.FindIndex(
    delegate(Employee employee)
    {
        return employee.LastName.Equals(somename, StringComparison.Ordinal);
    });
280Z28
I confirm this does the job. However the project doesn't use Linq. We are using .NET 2.0
Bay Wolf
@Bay: you can use the old (pre-C# 3) syntax for the anonymous delegates, but the original I wrote would work find in .NET 2 as long as you're compiling with the C# 3 compiler (C# 3.0 and .NET 3.0 are not dependent on each other).
280Z28
+7  A: 
public int FindIndex(Predicate<T> match);

Using lambdas:

employeeList.FindIndex(r => r.LastName.Equals("Something"));

Note:

// Returns:
//     The zero-based index of the first occurrence of an element that matches the
//     conditions defined by match, if found; otherwise, –1.

EDIT: Beaten

Seth
A: 

you can do this through override Equals method

class Employee
    {
        string _name;
        string _last;
        double _val;
        public Employee(string name, string last, double  val)
        {
            _name = name;
            _last = last;
            _val = val;
        }
        public override bool Equals(object obj)
        {
            Employee e = obj as Employee;
            return e._name == _name;
        }
    }
Ahmed Said
Ugly solution...
Kamarey
This is the one we used to do in the fisrt dotnet version
Ahmed Said
+2  A: 

Sorry, One more for good measure :)

int index = employees.FindIndex(delegate(Employee employee){return employee.LastName == "Something";});

Edit: - Full Example in .NET 2.0 Project.

class Program
{
    class Employee { public string LastName { get; set; } }
    static void Main(string[] args)
    {
        List<Employee> employeeList = new List<Employee>();
        employeeList.Add(new Employee(){LastName="Something"});
        employeeList.Add(new Employee(){LastName="Something Else"});
        int index = employeeList.FindIndex(delegate(Employee employee) { return employee.LastName.Equals("Something"); });
        Console.WriteLine("Index:{0}", index);
        Console.ReadKey();
    }
}
Wil P
Yes, this satisfies my actual request. I am leaving the check mark on the lambda version for other users as they might be using 3.5. Thanks.
Bay Wolf
Cool, glad it works.
Wil P
Your example uses the initializer syntax that's only in C# 3. The OP hasn't clearly distinguished whether he's using C# 3 or C# 2, so it's hard to tell what he wants.
280Z28