Flexible projection magic for EF Core
Starting with V2 of this project we're binding against EF Core 6. If you're targeting EF Core 5 or EF Core 3.1 then you can use the latest v1 release. These are functionally equivalent.
- Install the package from NuGet
- Enable Projectables in your DbContext by adding:
dbContextOptions.UseProjectables() - Mark properties, methods, or constructors with
[Projectable]. - Read the documentation for guides, reference, and recipes.
class Order
{
public decimal TaxRate { get; set; }
public ICollection<OrderItem> Items { get; set; }
[Projectable] public decimal Subtotal => Items.Sum(item => item.Product.ListPrice * item.Quantity);
[Projectable] public decimal Tax => Subtotal * TaxRate;
[Projectable] public decimal GrandTotal => Subtotal + Tax;
}
public static class UserExtensions
{
[Projectable]
public static Order GetMostRecentOrder(this User user) =>
user.Orders.OrderByDescending(x => x.CreatedDate).FirstOrDefault();
}
var result = dbContext.Users
.Where(u => u.UserName == "Jon")
.Select(u => new { u.GetMostRecentOrder().GrandTotal })
.FirstOrDefault();The properties are inlined into SQL — no client-side evaluation, no N+1.
There are two components: a Roslyn source generator that emits companion Expression<TDelegate> trees for each [Projectable] member at compile time, and a runtime interceptor that walks your LINQ queries and substitutes those expressions before EF Core translates them to SQL.
| Feature | Docs |
|---|---|
| Properties & methods | Guide → |
| Extension methods | Guide → |
| Constructor projections | Guide → |
| Method overloads | Fully supported |
Pattern matching (switch, is) |
Reference → |
| Block-bodied members (experimental) | Advanced → |
| Null-conditional rewriting | Reference → |
| Enum method expansion | Reference → |
UseMemberBody |
Reference → |
| Roslyn analyzers & code fixes (EFP0001–EFP0012) | Reference → |
| Limited/Full compatibility mode | Reference → |
No. The interceptor hooks into EF Core's query compilation pipeline before any provider-specific translation, so it works with SQL Server, PostgreSQL, SQLite, Cosmos DB, and any other EF Core provider.
Two compatibility modes are available: Full (default) expands every query before handing it to EF Core; Limited expands once and caches the result. Limited mode often outperforms plain EF Core on repeated queries. See the Compatibility Mode docs.
Yes — a [Projectable] member can call other [Projectable] members. They are recursively inlined into the final SQL.
How does this relate to Expressionify?
Expressionify has overlapping features and a similar approach but a narrower scope. Projectables adds constructor projections, pattern matching, block-bodied members, enum expansion, and a richer diagnostics layer.
LinqKit and similar libraries predate source generators. Projectables (and Expressionify) are superior approaches for modern .NET because the source generator does the heavy lifting at compile time with no runtime reflection.
- v1.x → EF Core 3.1 / 5
- v2.x–v3.x → EF Core 6 / 7
- v6.x+ → EF Core 6+ (current; targets
net8.0andnet10.0)