Projections & Streaming
Projections
Section titled “Projections”Project query results into a different shape at the SQL level via json_object — no full document deserialization needed.
// Flat projectionvar results = await store.Query<User, UserSummary>( u => u.Age == 25, u => new UserSummary { Name = u.Name, Email = u.Email }, ctx.User, ctx.UserSummary);
// Nested source propertiesvar results = await store.Query<Order, OrderSummary>( o => o.Status == "Shipped", o => new OrderSummary { Customer = o.CustomerName, City = o.ShippingAddress.City }, ctx.Order, ctx.OrderSummary);
// GetAll with projectionvar results = await store.GetAll<Order, OrderDetail>( o => new OrderDetail { Customer = o.CustomerName, LineCount = o.Lines.Count() }, ctx.Order, ctx.OrderDetail);
// Collection methods in projections: Count(), Any(), Count(predicate), Any(predicate)o => new OrderDetail { HasLines = o.Lines.Any(), GadgetCount = o.Lines.Count(l => l.ProductName == "Gadget") }Projection expressions reference
Section titled “Projection expressions reference”| Expression | SQL Output |
|---|---|
x => new R { A = x.Name } | json_object('name', json_extract(Data, '$.name')) |
x => new R { C = x.Nav.Prop } | json_object('c', json_extract(Data, '$.nav.prop')) |
x => new R { N = x.Lines.Count() } | json_array_length(Data, '$.lines') |
x => new R { N = x.Lines.Count(l => ...) } | (SELECT COUNT(*) FROM json_each(Data, '$.lines') WHERE ...) |
x => new R { B = x.Tags.Any() } | CASE WHEN json_array_length(...) > 0 THEN json('true') ELSE json('false') END |
x => new R { B = x.Tags.Any(t => ...) } | CASE WHEN EXISTS (...) THEN json('true') ELSE json('false') END |
Streaming
Section titled “Streaming”All list-returning methods have IAsyncEnumerable<T> streaming counterparts that yield results one-at-a-time without buffering.
// Stream all documentsawait foreach (var user in store.GetAllStream<User>(ctx.User)){ Console.WriteLine(user.Name);}
// Stream with expression filterawait foreach (var user in store.QueryStream<User>(u => u.Age > 30, ctx.User)){ Console.WriteLine(user.Name);}
// Stream with projectionawait foreach (var summary in store.GetAllStream<Order, OrderSummary>( o => new OrderSummary { Customer = o.CustomerName, City = o.ShippingAddress.City }, ctx.Order, ctx.OrderSummary)){ Console.WriteLine($"{summary.Customer} in {summary.City}");}
// Stream with raw SQLawait foreach (var user in store.QueryStream<User>( "json_extract(Data, '$.name') = @name", ctx.User, new { name = "Alice" })){ Console.WriteLine(user.Name);}