Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

342
Visualizações
C# 10 .Net 6 MinBy MaxBy Linq projection to single property

Is there a inline way to project properties after the new MinBy or MaxBy calls in C# 10 .Net 6?

double topPriceInList = prices.MaxBy(h => h.High)."Select(h => h.High)";

Certainly Visual Studio 2021 doesn't like any that I have tried.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

MaxBy(this IEnumerable<T>) and MinBy(this IEnumerable<T>) don't return IEnumerable<T> - they return single elements (i.e. T), so just do this:

double topPriceInList = prices.MaxBy(h => h.High).High;

That said, that code above doesn't need MaxBy, just use Max:

double topPriceInList = prices.Max(h => h.High);

// or:

double topPriceInList = prices.Select(h => h.High).Max();

  • Note that Min, Max, MinBy, and MaxBy will all throw an exception at runtime if the source collection is empty (or any previous .Where steps eliminated all elements)
  • ...so you may need to use MinOrDefault, MaxOrDefault, MaxByOrDefault, or MaxByOrDefault, in which case use the null-safe navigation operator ?. (aka elvis-operator).
    • But make sure the ...OrDefault() extension-method is invoked on IEnumerable<Nullable<T>> and not IEnumerable<T> because default(T) for value-types is never null which is a frequent source of bugs.
double? topPriceInListOrNull = prices.Select(h => (double?)h.High).MaxOrDefault();
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda