I am trying to figure out the best way to do what I thought would be easy. I have a database model called Line that represents a line in an invoice.
It looks roughly
I think easiest way to resolve this problem is using DelegateDecompiler.EntityFramework (make by Alexander Zaytsev)
It's a library which is able to decompile a delegate or a method body to its lambda representation.
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");
Async Support with EntityFramework
The DelegateDecompiler.EntityFramework
package provides DecompileAsync
extension method which adds support for EF's Async operations.
You can find 8 way to mix some property values together in EF
here:
Computed Properties and Entity Framework. (written by Dave Glick)