3

I've created a list of a class. Here you can see the code:

public class People{
        public string name;
        public int age;     
    }   

public List<People> peopleInfo = new List<People>();

My problem is that I have for example a name and now I want to know the age. How do I get this data? I can get a name or the age on a specific position by this:

int pos = peopleInfo[0].name;

But how I can do it the inverted way and get the position of name? When I have this it easy to get the age.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Eknoes
  • 349
  • 4
  • 6
  • 17
  • 1
    Is this an operation you need to do frequently, or just occasionally? Also, are you using this list to do a lot of other things? By either changing the data structure, or creating an additional data structure, that is more suited to this task you can make it both easier and more efficient. – Servy Jun 27 '13 at 18:42

3 Answers3

5

You can use the FindIndex method:

var searchForName = "John";
int index = peopleInfo.FindIndex(p => p.name == searchForName);

This will return the index of the first person in the list whose name property is equal to "John". Of course there may be many people with the name "John", and you may want to find all of them. For this you can use LINQ:

IEnumerable<int> indexes = peopleInfo.Select((p, i) => new { p, i })
                                     .Where(x => x.p.name == searchForName)
                                     .Select(x => x.i);
foreach(int i in indexes)
{
    peopleInfo[i].age ...
}

But if you don't really need the index, this is much more simple:

foreach(People person in peopleInfo.Where(p => p.name == searchForName))
{
    person.age ...
}
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • 1
    It's the lambda operator, part of making lambda expressions: http://msdn.microsoft.com/en-us/library/vstudio/bb397687.aspx – Mike Precup Jun 27 '13 at 18:43
  • 1
    @JesonPark It defines a [**lambda expression** (a.k.a. anonymous function)](http://msdn.microsoft.com/en-us/library/vstudio/bb397687.aspx) – p.s.w.g Jun 27 '13 at 18:44
4

You can use FindIndex to find person by name. But in case if this operation will be frequent, you may hit performance problems, as each FindIndex call will look up through entire list, one record by one.

Probably in this situation it would be better to build dictionary of persons by name. Or even dictionary of age by name, if the only thing you need is a person's age.

Also, consider case when person name is not unique.

List<People> peopleInfo = new List<People>() { ... };
Dictionary<string, People> map = peopleInfo.ToDictionary(p => p.name);
People p = map["John"];
Alexander
  • 4,153
  • 1
  • 24
  • 37
  • 3
    I'd recommend `ToLookup` if there are multiple people named `"John"`. Otherwise you'll get "*ArgumentException: An item with the same key has already been added.*" – p.s.w.g Jun 27 '13 at 18:45
1
int index = peopleInfo.FindIndex(p => p.name == "TheName");

or, you could just find the object directly...

People person = peopleInfo.FirstOrDefault(p => p.name == "TheName");
John Kraft
  • 6,811
  • 4
  • 37
  • 53