Here's what I want to do :
class MyDbContext : DbContext
{
private static Expression<Func<MyClass, int>> myExpression1 = x => /* something complicated ... */;
private static Expression<Func<Item, int>> myExpression2 = x => /* something else complicated ... */;
public object GetAllData()
{
return (
from o in MyClassDbSet.AsExpandable()
select new
{
data1 = myExpression1.Invoke(o), // problem 1
data2 = o.Items.Select(myExpression2.Compile()) // problem 2
}
);
}
}
UPDATE :
myExpression
has to stay separated from my query, because I want to reuse it in multiple LINQ queries.
UPDATE 2 :
Separated myExpression
into myExpression1
and myExpression2
to make clear the fact that I want to reuse them separately.
UPDATE 3 :
Added LINQkit to example.
Problem 1 throws : Unable to cast an object of type 'System.Linq.Expressions.FieldExpression' to type 'System.Linq.Expressions.LambdaExpression'.
Problem 2 throws : Internal .NET Framework Data Provider error 1025.