Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Need to return an object with the lowest property value from List of objects

I have a list of specific object that looks like this:

var customers = new List<Customer>
{
    new Customer { Name = "John", Age = 20 },
    new Customer { Name = "Adam", Age = 30 },
    new Customer { Name = "Joi", Age = 26 }
};

What is the best way to return the object of customer with the lowest age?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I believe you would achieve that by ordering objects by age first then returning the first element in the list.
and you can accomplish that by adding this linq query.

Customer lowestAgeCustomer = Customers.OrderBy(c => c.Age).FirstOrDefault();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading