EF Core Delegate Decompiler Use Computed Properties in LINQ

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

DelegateDecompiler.EntityFrameworkCore enables you to use computed properties in LINQ. Let's add a computed property FullName to the Author class.

public class Author
{
    public int AuthorId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    [Computed]
    public string FullName
    {
        get { return FirstName + " " + LastName; }
    }
    public virtual ICollection<Book> Books { get; set; }
}

Now you can use the computed property FullName in your LINQ queries, but make sure to call the Decompile() method as shown below.

using (var context = new BookStore())
{
    var author = context.Authors
        .Where(a => a.FullName == "Yan Li")
        .Decompile()
        .FirstOrDefault();
}

The Decompile() method decompiles your computed properties to their underlying representation and the query will become similar to the following query.

using (var context = new BookStore())
{
    var author = context.Authors
        .Where(a => (a.FirstName + " " + a.LastName) == "Yan Li")
        .FirstOrDefault();
}

If you have not used the [Computed] attribute on your computed property, you can use the Computed() extension method.

using (var context = new BookStore())
{
    var author = context.Authors
        .Where(a => a.FullName.Computed() == "Yan Li")
        .Decompile()
        .FirstOrDefault();
}


Got any EF Core Delegate Decompiler Question?