Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hello friends,

Suppose I have one class
"Person"
………

public class Person
   {
       public string Name { get; set; }
       public int Age { get; set; }
   }


and suppose I have list “listOfPerson” which contain say 15 person…

List<Person> listOfPerson = new List<Person>();


Now I trying to get Object person form this List with Max Age…..

Read Carefully I an not just need Max Age……
but entire object….so I can also access the name of the person with max Age…….


Thanks………
Posted

1 solution

Try this

Person maxAge = listOfPerson.OrderByDescending(p => p.Age).First();


Hope this helps
 
Share this answer
 
Comments
Pritesh Aryan 22-Apr-11 1:56am    
ya....surly.....
Now suppose.....
all the person with max age...(list of person with max Age from given list of person)?
what should be your answer?
Thanks..........
Wayne Gaylard 22-Apr-11 2:13am    
I assume you mean if more than one person has max age, then try this

var personsWithMaxAge = from person in listOfPerson where person.Age == (listOfPerson.Max(p => p.Age)) select person;
Pritesh Aryan 22-Apr-11 2:18am    
@Wayne, Thank you so much.......
Wayne Gaylard 22-Apr-11 2:19am    
Pleasure
Pritesh Aryan 22-Apr-11 2:23am    
@Wayne,, hello........
Is it possible to get List<int>. which contain all the ages of person in the given list?.......

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900