Path: redb/redb.Examples/Examples/E076_Distinct.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/E076_Distinct.cs
----
using System.Diagnostics;
using redb.Core;
using redb.Examples.Models;
using redb.Examples.Output;
namespace redb.Examples.Examples;
///
/// Get unique objects by Props hash using Distinct().
/// Server-side: SELECT DISTINCT ON (_hash) - deduplicates by Props content.
///
[ExampleMeta("E076", "Distinct - Unique by Props", "Query",
ExampleTier.Free, 76, "Distinct", "Unique", "Dedupe", RelatedApis = ["IRedbQueryable.Distinct"])]
public class E076_Distinct : ExampleBase
{
public override async Task RunAsync(IRedbService redb)
{
var sw = Stopwatch.StartNew();
// Server-side Distinct by Props hash
var query = redb.Query()
.Distinct()
.Take(100);
// Uncomment to see generated SQL:
// var sql = await query.ToSqlStringAsync();
// Console.WriteLine(sql);
var uniqueEmployees = await query.ToListAsync();
// Compare with total count
var totalCount = await redb.Query().CountAsync();
sw.Stop();
return Ok("E076", "Distinct - Unique by Props", ExampleTier.Free, sw.ElapsedMilliseconds,
uniqueEmployees.Count,
[$"Unique loaded: {uniqueEmployees.Count}", $"Total in DB: {totalCount}"]);
}
}