How to use IndexOf() method of List<object>

前端 未结 6 1657
遥遥无期
遥遥无期 2020-12-29 01:08

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

6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-29 01:58

    I prefer like this

        private List persons = List();
    
                public PersonService()
                {
                    persons = new List() { 
                        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))];
            }
    

提交回复
热议问题