C# 如何使用 List<object> 的 IndexOf() 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1568593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use IndexOf() method of List<object>
提问by omencat
All the examples I see of using the IndexOf()
method in List<T>
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.
我看到的所有使用IndexOf()
方法的例子List<T>
都是基本的字符串类型。我想知道的是如何根据对象变量之一返回作为对象的列表类型的索引。
List<Employee> employeeList = new List<Employee>();
employeeList.Add(new Employee("First","Last",45.00));
I want to find the index where employeeList.LastName == "Something"
我想找到索引在哪里 employeeList.LastName == "Something"
采纳答案by Sam Harwell
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):
编辑:C# 2.0 没有 lambdas(原始版本不使用 LINQ 或任何 .NET 3+ 功能,仅使用 C# 3.0 中的 lambda 语法):
int index = employeeList.FindIndex(
delegate(Employee employee)
{
return employee.LastName.Equals(somename, StringComparison.Ordinal);
});
回答by Chris
public int FindIndex(Predicate<T> match);
Using lambdas:
使用 lambda 表达式:
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.
回答by Ahmed Said
you can do this through override Equals method
你可以通过覆盖 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;
}
}
回答by Wil P
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.
编辑: - .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();
}
}
回答by PAWAN RAJ Shakya
I prefer like this
我更喜欢这样
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))];
}
回答by snr
The answer is for those coming here to know why
IndexOf()
doesn't work.
答案是让那些来到这里的人知道为什么
IndexOf()
不起作用。
Your class must overrideEquals
method of object
possessing the following declaration.
你的类必须覆盖Equals
的方法,object
它具有以下声明。
public override bool Equals(object obj)