Path: redb/redb.Examples/ExamplesEf/E140_DistinctByRedb_ef.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Examples/ExamplesEf/E140_DistinctByRedb_ef.cs
----
using System.Diagnostics;
using Microsoft.EntityFrameworkCore;
using redb.Examples.Ef;
using redb.Examples.Examples;
using redb.Examples.Output;
namespace redb.Examples.ExamplesEf;
///
/// EF Core twin of : up to 100 employees with distinct names, as whole objects.
/// EF has no DISTINCT ON; it groups by the name, keeps the smallest key of each group and loads by those keys.
///
[ExampleMeta("E140_ef", "EF: DistinctByRedb - By Field", "Analytics", ExampleTier.Pro, 1, "EF", "Distinct")]
public class E140_DistinctByRedb_ef : EfExampleBase
{
public override async Task RunEfAsync(IDbContextFactory contexts)
{
var sw = Stopwatch.StartNew();
await using var db = await contexts.CreateDbContextAsync();
var ids = await db.EmployeeProps
.GroupBy(e => e.objects._name)
.Select(g => g.Min(e => e._id))
.OrderBy(id => id)
.Take(100)
.ToListAsync();
var uniqueByName = await db.EmployeeProps.WithGraph()
.Where(e => ids.Contains(e._id))
.ToBusinessListAsync();
sw.Stop();
var names = string.Join(", ", uniqueByName.Take(3).Select(x => x.name));
return Ok("E140_ef", "EF: DistinctByRedb - By Field", ExampleTier.Pro, sw.ElapsedMilliseconds, uniqueByName.Count,
["GROUP BY objects._name, then load by key", $"First 3: {names}..."]);
}
}