19 November 2026

EF Core 11 MaxByAsync and MinByAsync: Find Highest Student Score

  Asp.Net CS By Example       19 November 2026

Suppose a student table contains names and scores. MaxAsync(s => s.Score) tells you the highest number, but sometimes you need the student who earned it. EF Core 11 can translate MaxByAsync and MinByAsync into a database query that returns the row with the highest or lowest selected value.

From scores to students

Asha 84 + Ravi 91 + Meena 72 → MaxBy: Ravi / MinBy: Meena

What do MaxByAsync and MinByAsync return?

MaxByAsync(s => s.Score) looks at each student's score and returns the student with the largest score. MinByAsync returns the student with the smallest score. On an empty table, the result is null. If several students tie, this example does not promise which tied row will be returned; add a separate ordering rule if your application needs a predictable tie-break.

Create a small SQLite practice app

Install the .NET 11 SDK. Create a console project with dotnet new console -n StudentScores -f net11.0, enter its folder, and add the database provider with dotnet add package Microsoft.EntityFrameworkCore.Sqlite --version 11.0.0. Replace Program.cs with this complete example:

// Requires the .NET 11 SDK and Microsoft.EntityFrameworkCore.Sqlite 11.0.0.
using Microsoft.EntityFrameworkCore;

await using var db = new SchoolDb();
await db.Database.EnsureCreatedAsync();

if (!await db.Students.AnyAsync())
{
    db.Students.AddRange(
        new Student { Name = "Asha", Score = 84 },
        new Student { Name = "Ravi", Score = 91 },
        new Student { Name = "Meena", Score = 72 });
    await db.SaveChangesAsync();
}

var highest = await db.Students.MaxByAsync(s => s.Score);
var lowest = await db.Students.MinByAsync(s => s.Score);

Console.WriteLine($"Highest: {highest?.Name} ({highest?.Score})");
Console.WriteLine($"Lowest: {lowest?.Name} ({lowest?.Score})");

public sealed class SchoolDb : DbContext
{
    public DbSet<Student> Students => Set<Student>();

    protected override void OnConfiguring(DbContextOptionsBuilder options) =>
        options.UseSqlite("Data Source=student-scores.db");
}

public sealed class Student
{
    public int Id { get; set; }
    public string Name { get; set; } = "";
    public int Score { get; set; }
}

Run dotnet run. On a fresh student-scores.db database, the expected output is Highest: Ravi (91) and Lowest: Meena (72). The program seeds three rows only when the table is empty, so a second run uses the existing database. For a clean repeat, use a new practice folder or a disposable copy of the database.

Why this is a database query

The query begins with db.Students, an EF Core DbSet. EF Core 11 translates these operators for relational queries, so the database can choose a row without loading every student into your application first. By contrast, calling ToListAsync() before MaxBy loads rows into memory and then chooses locally. For a large table, inspect the generated query and add an index for the score column if the workload needs one.

Practice

  1. Add a student with score 99. Predict the new highest result.
  2. Change Score to Age and explain what each method would now find.
  3. Decide what your app should show when the table has no students.

Continue with the .NET student learning path and review our EF Core with SQLite starter lesson. Microsoft documents the new translations in What's New in EF Core 11.

logoblog

Thanks for reading EF Core 11 MaxByAsync and MinByAsync: Find Highest Student Score

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.