Path: redb/redb.Examples/ExamplesEf/E149_SelectProjection_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/E149_SelectProjection_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 : three fields of 100 employees, then the same 100 as
/// whole objects. As in redb, the measured time covers both.
///
[ExampleMeta("E149_ef", "EF: Select - Projection", "Query", ExampleTier.Free, 1, "EF")]
public class E149_SelectProjection_ef : EfExampleBase
{
public override async Task RunEfAsync(IDbContextFactory contexts)
{
var sw = Stopwatch.StartNew();
await using var db = await contexts.CreateDbContextAsync();
var projected = await db.EmployeeProps
.Take(100)
.Select(x => new
{
x._id,
x.FirstName,
x.Salary,
HomeCity = x.EmployeeProps_HomeAddress!.City,
HomeFloor = x.EmployeeProps_HomeAddress!.EmployeeProps_HomeAddress_Building!.Floor
})
.ToListAsync();
// OrderBy(_id) is EF's own need: with split queries and Take, every child query must pick the same rows.
var swFull = Stopwatch.StartNew();
var full = await db.EmployeeProps.WithGraph().OrderBy(e => e._id).Take(100).ToBusinessListAsync();
swFull.Stop();
sw.Stop();
var first = projected.FirstOrDefault();
return Ok("E149_ef", "EF: Select - Projection", ExampleTier.Free, sw.ElapsedMilliseconds, projected.Count,
[$"Projected 5 fields, 2 of them nested: {sw.ElapsedMilliseconds - swFull.ElapsedMilliseconds}ms",
$"Full load: {swFull.ElapsedMilliseconds}ms ({full.Count} objects)",
first != null ? $"First: {first.FirstName} ${first.Salary:N0}, home {first.HomeCity}, floor {first.HomeFloor}" : "No data"]);
}
}