1

I have a list of objects(People) these people have certain attributes with them, e/g name, age, DOB etc. How would I do about searching for a certain person by their name and then returning the rest of the data belonging to that person.

I would just like some hints on what to look up been really struggling for a few days with it now and for the life cant think of how to do it.

4

1 に答える 1

2

Linqは最も読みやすい imho です。

var alfreds = allPersons.Where(p => p.Name == "Alfred");

foreach(Person p in alfreds)
{
    Console.WriteLine("Next {0} Age:{1} DOB:{2}", p.Name, p.Age, p.DOB);
}

正確に 1 人を探している場合、または最初の "Alfred" を取得したい場合 (例):

Person p = allPersons.FirstOrDefault(p => p.Name == "Alfred");
if(p != null)
{
    Console.WriteLine("Next {0} Age:{1} DOB:{2}", p.Name, p.Age, p.DOB);
}
于 2013-03-19T16:22:54.630 に答える