Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

186
Views
Chain Max and Where in Linq

Given this code:

Book[] books = new Book[3]
    {
        new Book("100", "Title1"),
        new Book("200", "Title2"),
        new Book("200", "Title3"),
    };

getBookWithMostPages() {
    var max = books.Max(book => book.pages);
    return books.FirstOrDefault((book) => book.pages == max);
}

Is it possible (there is any documented way) to chain Max and FirstOrDefault and return the same result ?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

.NET 6 introduced the MaxBy operator. I think that might be what you're looking for:

var book = books.MaxBy(b => b.Pages);

Here's the implementation in the reference source so if you're using the older version of .NET you should be able to just copy it into your codebase. If of course that's what you want...

over 4 years ago · Santiago Trujillo Report

0

You can order the array and return the first value:

return books.OrderByDescending(book => book.pages)
            .FirstOrDefault();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!