DelegateDecompiler.EntityFramework 0.34.0

DelegateDecompiler

https://ci.appveyor.com/project/hazzik/delegatedecompiler/branch/main https://nuget.org/packages/DelegateDecompiler

A library that is able to decompile a delegate or a method body to their lambda representation

Sponsorship

If you like the library please consider supporting my work.

Examples

Using computed properties in linq.

Asume we have a class with a computed property

class Employee
{
    [Computed]
    public string FullName => FirstName + " " + LastName;
    public string LastName { get; set; }
    public string FirstName { get; set; }
}

And you are going to query employees by their full names

var employees = (from employee in db.Employees
                 where employee.FullName == "Test User"
                 select employee).Decompile().ToList();

When you call .Decompile method it decompiles your computed properties to their underlying representation and the query will become simmilar to the following query

var employees = (from employee in db.Employees
                 where (employee.FirstName + " " + employee.LastName)  == "Test User"
                 select employee).ToList();

If your class doesn't have a [Computed] attribute, you can use the .Computed() extension method..

var employees = (from employee in db.Employees
                 where employee.FullName.Computed() == "Test User"
                 select employee).ToList();

Also, you can call methods that return a single item (Any, Count, First, Single, etc) as well as other methods in identical way like this:

bool exists = db.Employees.Decompile().Any(employee => employee.FullName == "Test User");

Again, the FullName property will be decompiled:

bool exists = db.Employees.Any(employee => (employee.FirstName + " " + employee.LastName) == "Test User");

Limitations

Not every compiled code can be represented as a lambda expression. Some cases are explicitly not supported, other can break and produce unexpected results.

Some of the known cases listed below

Loops

Loops often cannot be represented as an expression tree.

So, the following imperative code would probably throw a StackOverflowException:

var total = 0;
foreach (var item in this.Items) { total += item.TotalPrice; }
return total;

Instead, write it as a declarative Linq expression, which would be supported.

return this.Items.Sum(i => i.TotalPrice);

Recursion and self-referencing

Recursion and self-referencing of computed properties cannot be represented as an Expression tree, and would probably throw StackOverflowException similarly to loops.

Using with EntityFramework and other ORMs

If you are using ORM specific features, like EF's Include, AsNoTracking or NH's Fetch then Decompile method should be called after all ORM specific methods, otherwise it may not work. Ideally use Decompile extension method just before materialization methods such as ToList, ToArray, First, FirstOrDefault, Count, Any, and etc.

Async Support with EntityFramework 6

The DelegateDecompiler.EntityFramework package provides DecompileAsync extension method which adds support for EF's Async operations.

Async Support with EntityFramework Core 5.0 and later

The DelegateDecompiler.EntityFrameworkCore5 package provides DecompileAsync extension method which adds support for EF's Async operations.

Automatic decompilation

You can configure DelegateDecompiler to automatically decompile all EF Core queries:

public class YourDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder options) {
        options.AddDelegateDecompiler();
        // Other configuration
    }
}

With this approach you would not need to call Decompile or DecompileAsync methods on queries.

Installation

Available on NuGet

License

MIT license - http://opensource.org/licenses/mit

Showing the top 20 packages that depend on DelegateDecompiler.EntityFramework.

Packages Downloads
AutoMapper.EF6
Extensions to make AutoMapper easier to work with Entity Framework. Project to collections and items, decompiling calculated properties along the way
75
AutoMapper.EF6
Extensions to make AutoMapper easier to work with Entity Framework. Project to collections and items, decompiling calculated properties along the way
10
AutoMapper.EF6
Extensions to make AutoMapper easier to work with Entity Framework. Project to collections and items, decompiling calculated properties along the way
9
AutoMapper.EF6
Extensions to make AutoMapper easier to work with Entity Framework. Project to collections and items, decompiling calculated properties along the way
8
AutoMapper.EF6
Extensions to make AutoMapper easier to work with Entity Framework. Project to collections and items, decompiling calculated properties along the way
7
AutoMapper.EF6
Extensions to make AutoMapper easier to work with Entity Framework. Project to collections and items, decompiling calculated properties along the way
6
AutoMapper.EF6
Extensions to make AutoMapper easier to work with Entity Framework. Project to collections and items, decompiling calculated properties along the way
5

.NET Framework 4.5

.NET Standard 2.1

Version Downloads Last updated
0.34.0 3 05/29/2024
0.33.1 3 05/15/2024
0.33.0 3 05/15/2024
0.32.0 3 06/13/2023
0.31.0 3 06/14/2023
0.31.0-artifacts.30 6 06/11/2023
0.31.0-alpha.2 3 06/11/2023
0.31.0-alpha.1 4 06/11/2023
0.30.0 6 07/18/2022
0.29.1 4 06/13/2023
0.29.0 2 06/14/2023
0.28.3 3 01/17/2021
0.28.2 4 06/13/2023
0.28.1 2 02/19/2024
0.28.0 5 06/13/2023
0.27.0 2 06/13/2023
0.26.1 5 06/13/2023
0.25.0 2 06/14/2023
0.24.0 2 12/30/2023
0.23.0 565 11/14/2019
0.22.0 5 06/14/2023
0.21.0 2 06/13/2023
0.20.0 2 06/13/2023
0.19.0 3 02/18/2024
0.18.1 1 02/19/2024
0.18.0 2 06/12/2023
0.17.0 3 06/14/2023
0.16.0 1 02/19/2024
0.15.1 5 02/19/2024
0.15.0 3 06/13/2023
0.14.1 3 06/13/2023
0.13.1 6 06/13/2023
0.13.0 4 06/14/2023
0.12.1 3 12/18/2023