Use LINQ MaxBy() and MinBy()
OpenRewrite.Recipes.Net6.UseLinqMaxMinBy
Replace collection.OrderByDescending(selector).First() with collection.MaxBy(selector) and collection.OrderBy(selector).First() with collection.MinBy(selector). Also handles .Last() variants (OrderBy().Last() → MaxBy, OrderByDescending().Last() → MinBy). Note: MinBy/MaxBy return default for empty reference-type sequences instead of throwing. Available since .NET 6.
Tags
Recipe source
This recipe is only available to users of Moderne.
This recipe is available under the Moderne Proprietary License.
Used by
This recipe is used as part of the following composite recipes:
Usage
In order to run C# recipes, you will need to use the Moderne CLI.
Once the CLI is installed, you can install this C# recipe package by running the following command:
mod config recipes nuget install OpenRewrite.MigrateDotNet
Then, you can run the recipe via:
mod run . --recipe OpenRewrite.Recipes.Net6.UseLinqMaxMinBy