Path: redb/redb.Examples/ExamplesEf/E157_DateTimeYear_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/E157_DateTimeYear_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 : objects created this year and older than 30, up to 100 whole
/// objects, and the count of this year's objects. The creation date is a field of the objects header.
///
[ExampleMeta("E157_ef", "EF: WhereRedb - DateTime.Year", "Query", ExampleTier.Pro, 1, "EF", "Where", "DateTime")]
public class E157_DateTimeYear_ef : EfExampleBase
{
public override async Task RunEfAsync(IDbContextFactory contexts)
{
var sw = Stopwatch.StartNew();
await using var db = await contexts.CreateDbContextAsync();
var currentYear = DateTime.UtcNow.Year;
var thisYear = db.EmployeeProps.Where(e => e.objects._date_create.Year == currentYear);
var results = await thisYear.Where(e => e.Age > 30)
.WithGraph().OrderBy(e => e._id).Take(100).ToBusinessListAsync();
var totalCount = await thisYear.CountAsync();
sw.Stop();
var dates = results.Take(3).Select(e => e.date_create.ToString("yyyy-MM-dd"));
return Ok("E157_ef", "EF: WhereRedb - DateTime.Year", ExampleTier.Pro, sw.ElapsedMilliseconds, totalCount,
[$"Filter: DateCreate.Year == {currentYear}",
$"Found: {totalCount} objects created this year",
$"Dates: {string.Join(", ", dates)}"]);
}
}