EntityFrameworkCore.Projectables 2.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package EntityFrameworkCore.Projectables --version 2.0.2
NuGet\Install-Package EntityFrameworkCore.Projectables -Version 2.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EntityFrameworkCore.Projectables" Version="2.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EntityFrameworkCore.Projectables --version 2.0.2
#r "nuget: EntityFrameworkCore.Projectables, 2.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EntityFrameworkCore.Projectables as a Cake Addin
#addin nuget:?package=EntityFrameworkCore.Projectables&version=2.0.2

// Install EntityFrameworkCore.Projectables as a Cake Tool
#tool nuget:?package=EntityFrameworkCore.Projectables&version=2.0.2

EntitiyFrameworkCore.Projectables

Flexible projection magic for EFCore

NuGet version (EntityFrameworkCore.Projectables) .NET

NuGet packages

  • EntityFrameworkCore.Projectables.Abstractions NuGet version NuGet
  • EntityFrameworkCore.Projectables NuGet version NuGet

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.

Getting started

  1. Install the package from NuGet
  2. Enable Projectables in your DbContext by calling: dbContextOptions.UseProjectables()
  3. Implement projectable properties and methods and mark them with the [Projectable] attribute.
  4. View our samples and checkout our Blog Post

Example

Assuming this sample:

class Order {
    public int Id { get; set; }
    public int UserId { get; set; }
    public DateTime CreatedDate { get; set; }

    public decimal TaxRate { get; set; }
    
    public User User { 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 GetMostRecentOrderForUser(this User user, DateTime? cutoffDate) => 
        user.Orders
            .Where(x => cutoffDate == null || x.CreatedDate >= cutoffDate)
            .OrderByDescending(x => x.CreatedDate)
            .FirstOrDefault();
}

var result = _dbContext.Users
    .Where(x => x.UserName == "Jon")
    .Select(x => new {
        x.GetMostRecentOrderForUser(DateTime.UtcNow.AddDays(-30)).GrandTotal
    });
    .FirstOrDefault();

The following query gets generated (assuming SQL Server as a database provider)

DECLARE @__sampleUser_UserName_0 nvarchar(4000) = N'Jon';

SELECT (
    SELECT COALESCE(SUM([p].[ListPrice] * CAST([o].[Quantity] AS decimal(18,2))), 0.0)
    FROM [OrderItem] AS [o]
    INNER JOIN [Products] AS [p] ON [o].[ProductId] = [p].[Id]
    WHERE (
        SELECT TOP(1) [o0].[Id]
        FROM [Orders] AS [o0]
        WHERE ([u].[Id] = [o0].[UserId]) AND [o0].[FulfilledDate] IS NOT NULL
        ORDER BY [o0].[CreatedDate] DESC) IS NOT NULL AND ((
        SELECT TOP(1) [o1].[Id]
        FROM [Orders] AS [o1]
        WHERE ([u].[Id] = [o1].[UserId]) AND [o1].[FulfilledDate] IS NOT NULL
        ORDER BY [o1].[CreatedDate] DESC) = [o].[OrderId])) * (
    SELECT TOP(1) [o2].[TaxRate]
    FROM [Orders] AS [o2]
    WHERE ([u].[Id] = [o2].[UserId]) AND [o2].[FulfilledDate] IS NOT NULL
    ORDER BY [o2].[CreatedDate] DESC) AS [GrandTotal]
FROM [Users] AS [u]
WHERE [u].[UserName] = @__sampleUser_UserName_0

Projectable properties and methods have been inlined! the generated SQL could be improved but this is what EFCore (v5) gives us.

How it works

Essentially there are 2 components: We have a source generator that is able to write companion Expression for properties and methods marked with the Projectable attribute. We then have a runtime component that intercepts any query and translates any call to a property or method marked with the Projectable attribute and translates the query to use the generated Expression instead.

FAQ

Are there currently any known limitations?

There is currently no support for overloaded methods. Each method name needs to be unique within a given type.

Is this specific to a database provider?

No; The runtime component injects itself within the EFCore query compilation pipeline and thus has no impact on the database provider used. Of course you're still limited to whatever your database provider can do.

Are there performance implications that I should be aware of?

There are 2 compatibility modes: Limited and Full. Most of the time, limited compatibility mode is sufficient however if you are running into issues with failed query compilation, then you may want to try Full compatibility mode. With Full compatibility mode, Each Query will first be expanded (Any calls to Projectable properties and methods will be replaced by their respective Expression) before being handed off to EFCore. (This is similar to how LinqKit/LinqExpander/Expressionify works). Because of this additional step, there is a small performance impact. Limited compatibility mode is smart about things and only expands the Query after it has been accepted by EF. The expanded query will then be stored in the Query Cache. With Limited compatibility you will likely see increased performance over EFCore without projectables. I have a planned post coming up talking about why that is but for now you can see it for yourself by running the included Benchmark.

Can I call additional properties and methods from my Projectable properties and methods?

Yes you can! Any projectable property/method can call into other properties and methods as long as those properties/methods are native to EFCore or as long as they are marked with a Projectable attribute.

Can I use projectable extensions methods on non-entity types?

Yes you can. It's perfectly acceptable to have the following code:

[Projectable]
public static int Squared(this int i) => i * i;

Any call to squared given any int will perfectly translate to SQL.

How do I deal with nullable properties

Expressions and Lamdas are different and not equal. Expressions can only express a subset of valid CSharp statements that are allowed in lambda's and arrow functions. One obvious limitation is the null-conditional operator. Consider the following example:

[Projectable]
public static string? GetFullAddress(this User? user) => user?.Location?.AddressLine1 + " " + user?.Location.AddressLine2;

This is a perfectly valid arrow function but it can't be translated directly to an expression tree. This Project will generate an error by default and suggest 2 solutions: Either you rewrite the function to explicitly check for nullables or you let the generator do that for you!

Starting from the official release of V2, we can now hint the generator in how to translate this arrow function to an expression tree. We can say:

[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Ignore)]

which will simply generate an expression tree that ignores the null-conditional operator. This generates:

user.Location.AddressLine1 + " " + user.Location.AddressLine2

This is perfect for a database like SQL Server where nullability is implicit and if any of the arguments were to be null, the resulting value will be null. If you are dealing with CosmosDB (which may result to client-side evaluation) or want to be explicit about things. You can configure your projectable as such:

[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Rewrite)]

This will rewrite your expression to explicitly check for nullables. In the former example, this will be rewritten to:

(user != null ? user.Location != null ? user.Location?.AddressLine 1 + (user != null ? user.Location != null ? user.Location.AddressLine2 : null) : null)

Note that using rewrite (not ignore) may increase the actual SQL query complexity being generated with some database providers such as SQL Server

How does this relate to Expressionify?

Expressionify is a project that was launched before this project. It has some overlapping features and uses similar approaches. When I first published this project, I was not aware of its existence so shame on me. Currently Expressionify targets a more focusses scope of what this project is doing and thereby it seems to be more limiting in its capabilities. Check them out though!

How does this relate to LinqKit/LinqExpander/...?

There are a few projects like LinqKit that were created before we had code generators in dotnet. These are great options if you're stuck with classical EF or don't want to rely on code generation. Otherwise I would suggest that EntityFrameworkCore.Projectables and Expresssionify are superior approaches as they are able to rely on SourceGenerators to do most of the hard work.

Is the available for EFCore 3.1, 5 and 6?

Yes it is! there is no difference between any of these versions and you can upgrade/downgrade whenever you want.

What is next for this project?

TBD... However one thing I'd like to improve is our Expression generation logic as its currently making a few assumptions (have yet to experience it breaking). Community contributions are very welcome!

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (3)

Showing the top 3 popular GitHub repositories that depend on EntityFrameworkCore.Projectables:

Repository Stars
mehdihadeli/food-delivery-microservices
🍔 A practical food delivery microservices, built with .Net 7, MassTransit, Domain-Driven Design, CQRS, Vertical Slice Architecture, Event-Driven Architecture, and the latest technologies.
EntityGraphQL/EntityGraphQL
A GraphQL library for .NET
zoriya/Kyoo
A portable and vast media library solution.
Version Downloads Last updated
4.0.0-preiew.1 64 3/8/2024
3.0.4 105,789 8/30/2023
3.0.3 76,621 5/4/2023
3.0.2 33,110 3/16/2023
3.0.1 945 3/9/2023
3.0.0 3,240 3/2/2023
3.0.0-beta.1 5,093 10/13/2022
2.3.1-beta.1 1,752 10/5/2022
2.3.0 72,994 8/3/2022
2.3.0-beta.1 115 7/8/2022
2.2.0 5,520 4/28/2022
2.1.1 5,403 1/12/2022
2.1.0 267 1/8/2022
2.0.4 434 1/3/2022
2.0.3 244 1/3/2022
2.0.2 2,327 11/26/2021
2.0.1 3,325 11/25/2021
2.0.0 318 11/15/2021
2.0.0-beta.1 144 9/16/2021
1.0.1 10,971 7/1/2021
1.0.0 325 6/30/2021
0.4.0 1,692 6/6/2021
0.3.0 332 6/4/2021
0.2.0 383 6/2/2021
0.1.0 486 6/1/2021