Path: redb/redb.Examples/Examples/E149_SelectProjection.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/E149_SelectProjection.cs
----
using System.Diagnostics;
using redb.Core;
using redb.Examples.Models;
using redb.Examples.Output;
namespace redb.Examples.Examples;
///
/// Select projection - load only specific fields instead of full object.
/// Reduces data transfer and improves performance.
///
[ExampleMeta("E149", "Select - Projection", "Query",
ExampleTier.Free, 149, "Select", "Projection", "Performance", RelatedApis = ["IRedbQueryable.Select"])]
public class E149_SelectProjection : ExampleBase
{
public override async Task RunAsync(IRedbService redb)
{
var sw = Stopwatch.StartNew();
// Project only 3 fields from 20+ available
var projected = await redb.Query()
.Take(100)
.Select(x => new
{
x.id,
x.Props.FirstName,
x.Props.Salary,
HomeCity = x.Props.HomeAddress!.City,
HomeFloor = x.Props.HomeAddress!.Building!.Floor
})
.ToListAsync();
// Compare with full load
var swFull = Stopwatch.StartNew();
var full = await redb.Query().Take(100).ToListAsync();
swFull.Stop();
sw.Stop();
var first = projected.FirstOrDefault();
return Ok("E149", "Select - Projection", ExampleTier.Free, sw.ElapsedMilliseconds, projected.Count,
[$"Projected 5 fields, 2 of them nested: {sw.ElapsedMilliseconds}ms",
$"Full load 20+ fields: {swFull.ElapsedMilliseconds}ms",
first != null ? $"First: {first.FirstName} ${first.Salary:N0}, home {first.HomeCity}, floor {first.HomeFloor}" : "No data"]);
}
}