如何使用List ]的IndexOf()方法 ] 我看到的在IndexOf()中使用List<T>方法的所有示例都是基本字符串类型。我想知道的是如何基于对象变量之一返回作为对象的列表类型的索引。 List<Employee> employeeList = new List<Employee>(); employeeList.Add(new Employee("First","Last",45.00)); 我想在employeeList.LastName == "Something"的位置找到索引> 我看到的在List 中使用IndexOf()方法的所有示例都是基本的字符串类型。我想知道的是如何基于......> ] <<<int index = employeeList.FindIndex(employee => employee.LastName.Equals(somename, StringComparison.Ordinal)); 编辑:没有针对C#2.0的lambda(原始语言不使用LINQ或任何.NET 3+功能,仅使用C#3.0中的lambda语法): int index = employeeList.FindIndex( delegate(Employee employee) { return employee.LastName.Equals(somename, StringComparison.Ordinal); }); public int FindIndex(Predicate<T> match); 使用lambda: employeeList.FindIndex(r => r.LastName.Equals("Something")); 注意: // Returns: // The zero-based index of the first occurrence of an element // that matches the conditions defined by match, if found; // otherwise, –1. 您可以通过覆盖Equals方法执行此操作 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; } } 抱歉,请再说一次:) int index = employees.FindIndex( delegate(Employee employee) { return employee.LastName == "Something"; }); 编辑:-.NET 2.0项目中的完整示例。 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(); } } 我喜欢这样 private List<Person> persons = List<Person>(); public PersonService() { persons = new List<Person>() { new Person { Id = 1, DOB = DateTime.Today, FirstName = "Pawan", LastName = "Shakya" }, new Person { Id = 2, DOB = DateTime.Today, FirstName = "Bibek", LastName = "Pandey" }, new Person { Id = 3, DOB = DateTime.Today, FirstName = "Shrestha", LastName = "Prami" }, new Person { Id = 4, DOB = DateTime.Today, FirstName = "Monika", LastName = "Pandey" }, }; } public PersonRepository.Interface.Person GetPerson(string lastName) { return persons[persons.FindIndex(p=>p.LastName.Equals(lastName, StringComparison.OrdinalIgnoreCase))]; } 答案是给那些来这里的人知道为什么IndexOf()不起作用。 您的类必须 override Equals的object方法具有以下声明。 public override bool Equals(object obj)

问题描述 投票:46回答:6
]

我看到的在IndexOf()中使用List<T>方法的所有示例都是基本字符串类型。我想知道的是如何基于对象变量之一返回作为对象的列表类型的索引。

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

我想在employeeList.LastName == "Something"的位置找到索引>

我看到的在List 中使用IndexOf()方法的所有示例都是基本的字符串类型。我想知道的是如何基于......>

]
<<<int index = employeeList.FindIndex(employee => employee.LastName.Equals(somename, StringComparison.Ordinal));
编辑:没有针对C#2.0的lambda(原始语言不使用LINQ或任何.NET 3+功能,仅使用C#3.0中的lambda语法):
int index = employeeList.FindIndex(
    delegate(Employee employee)
    {
        return employee.LastName.Equals(somename, StringComparison.Ordinal);
    });

public int FindIndex(Predicate<T> match);
使用lambda:
employeeList.FindIndex(r => r.LastName.Equals("Something"));

注意:

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

您可以通过覆盖Equals方法执行此操作
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; } }

抱歉,请再说一次:)
int index = employees.FindIndex( delegate(Employee employee) { return employee.LastName == "Something"; });

编辑:-.NET 2.0项目中的完整示例。

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

我喜欢这样
private List<Person> persons = List<Person>(); public PersonService() { persons = new List<Person>() { new Person { Id = 1, DOB = DateTime.Today, FirstName = "Pawan", LastName = "Shakya" }, new Person { Id = 2, DOB = DateTime.Today, FirstName = "Bibek", LastName = "Pandey" }, new Person { Id = 3, DOB = DateTime.Today, FirstName = "Shrestha", LastName = "Prami" }, new Person { Id = 4, DOB = DateTime.Today, FirstName = "Monika", LastName = "Pandey" }, }; } public PersonRepository.Interface.Person GetPerson(string lastName) { return persons[persons.FindIndex(p=>p.LastName.Equals(lastName, StringComparison.OrdinalIgnoreCase))]; }

答案是给那些来这里的人知道为什么IndexOf()不起作用。
您的类必须

override

Equalsobject方法具有以下声明。

public override bool Equals(object obj)

c# list indexof
6个回答
22
投票
使用lambda:
employeeList.FindIndex(r => r.LastName.Equals("Something"));

10
投票
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; } }

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

编辑:-.NET 2.0项目中的完整示例。


1
投票
private List<Person> persons = List<Person>(); public PersonService() { persons = new List<Person>() { new Person { Id = 1, DOB = DateTime.Today, FirstName = "Pawan", LastName = "Shakya" }, new Person { Id = 2, DOB = DateTime.Today, FirstName = "Bibek", LastName = "Pandey" }, new Person { Id = 3, DOB = DateTime.Today, FirstName = "Shrestha", LastName = "Prami" }, new Person { Id = 4, DOB = DateTime.Today, FirstName = "Monika", LastName = "Pandey" }, }; } public PersonRepository.Interface.Person GetPerson(string lastName) { return persons[persons.FindIndex(p=>p.LastName.Equals(lastName, StringComparison.OrdinalIgnoreCase))]; }

0
投票
答案是给那些来这里的人知道为什么IndexOf()不起作用。
您的类必须

override

© www.soinside.com 2019 - 2024. All rights reserved.