Skip to main content
 首页 » 编程设计

C# 查找类列表的索引

2024年02月13日18shanyou

查找类的索引:

我知道找到列表索引的唯一方法是

        int index = listEmployee.FindIndex( 
            delegate(Employee findEmployee) 
            { 
                return findEmployee.Name.Equals(findName, StringComparison.Ordinal); 
            }); 

我想知道如何添加使用选项

int indexT = listEmployee.FindIndex(r >= r.Name == findName); 

或者基本上我做错了什么我不能使用它。

class Employee 
{ 
    private string _name; private int _idNumber;  
    private string _deptarment; private string _position; 
 
    public Employee() 
    { 
        _name = ""; _idNumber = 0; _deptarment = ""; _position = ""; 
    } 
    public string Name 
    { 
        get { return _name; } 
        set { _name = value; } 
    } 
    public int IdNumber 
    { 
        get { return _idNumber; } 
        set { _idNumber = value; } 
    } 
    public string Department 
    { 
        get { return _deptarment; } 
        set { _deptarment = value; } 
    } 
    public string Position 
    { 
        get { return _position; } 
        set { _position = value; } 
    } 
} 

请您参考如下方法:

I was wondering how to add the option to use

int indexT = listEmployee.FindIndex(r >= r.Name == findName); 

很好,除了 r >= 处的语法问题应该是 r =>

所以这是可行的:

int indexT = listEmployee.FindIndex(r => r.Name == findName); 

参见:Lambda Expressions