Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

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

I have a list of Customers 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?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

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();
over 4 years ago · Santiago Trujillo Denunciar

0

This is most efficient way:

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

var youngest = customers.Aggregate((x, y) => x.Age < y.Age ? x : y);

That gives me:

youngest

Or, now with .NET 6.0:

var youngest = customers.MinBy(c => c.Age);
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda