← All recipes
Moderne licensed

Use LINQ MaxBy() and MinBy()

Recipe IDOpenRewrite.Recipes.CSharp.Migration.Dotnet.Net6.UseLinqMaxMinByArtifactOpenRewrite.Recipes.CSharp.Migration.Dotnet

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.

Single recipemodernizationcsharpdotnetProprietary

Usage

This recipe has no required configuration options. You’ll need the Moderne CLI configured before running the command below.

mod run . --recipe OpenRewrite.Recipes.CSharp.Migration.Dotnet.Net6.UseLinqMaxMinBy

If the recipe isn’t available locally, install it with:

mod config recipes nuget install OpenRewrite.Recipes.CSharp.Migration.Dotnet