Path: redb/redb.Examples/Examples/E140_DistinctByRedb.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/Examples/E140_DistinctByRedb.cs
----
using System.Diagnostics;
using redb.Core;
using redb.Examples.Models;
using redb.Examples.Output;
namespace redb.Examples.Examples;
///
/// DISTINCT ON specific base field.
/// Get one object per unique Name (first by Id).
///
[ExampleMeta("E140", "DistinctByRedb - By Field", "Analytics",
ExampleTier.Pro, 140, "Distinct", "DistinctByRedb", "Unique", RelatedApis = ["IRedbQueryable.DistinctByRedb"])]
public class E140_DistinctByRedb : ExampleBase
{
public override async Task RunAsync(IRedbService redb)
{
var sw = Stopwatch.StartNew();
var query = redb.Query()
.DistinctByRedb(x => x.Name)
.Take(100);
// Uncomment to see generated SQL:
//var sql = await query.ToSqlStringAsync();
//Console.WriteLine(sql);
var uniqueByName = await query.ToListAsync();
sw.Stop();
var names = string.Join(", ", uniqueByName.Take(3).Select(x => x.name));
return Ok("E140", "DistinctByRedb - By Field", ExampleTier.Pro, sw.ElapsedMilliseconds, uniqueByName.Count,
[$"DISTINCT ON (o._name)", $"First 3: {names}..."]);
}
}