xuzhg / DelegateDecompiler

A library which is able to decompile a delegate or a method body to its lambda representation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DelegateDecompiler

A tool which is able to decompile a delegate or a method body to its lambda representation

Examples

Using computed properties in linq.

Asume we have a class with a computed property

class Employee
{
    [Computed]
    public string FullName
    {
        get { return 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");

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 2.0-3.1

The DelegateDecompiler.EntityFrameworkCore 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.

Installation

Available on NuGet

Support

If you like the library please support my work.

License

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

About

A library which is able to decompile a delegate or a method body to its lambda representation

License:MIT License


Languages

Language:C# 95.7%Language:Visual Basic .NET 4.1%Language:Batchfile 0.3%