i need to dinamically generate expression like this:
Expression<Func<MyClass, bool>> expr = x => (x.SomeField.CompareTo(someValue) <= 0);
trying to do it like this:
var paramExpr = Expression.Parameter(typeof(MyClass), "x");
Expression<Func<MyClass, FieldType>> pathToField = x => x.SomeField;
Expression path = pathToField;
if (!(path is LambdaExpression lambdaMember))
throw ...;
Expression valueExpr = Expression.Constant(someValue);
var bodyExpr = Expression.LessThanOrEqual(Expression.Call(lambdaMember.Body, "CompareTo", null, valueExpr ), Expression.Constant(0));
return Expression.Lambda<Func<MyClass, FieldType>>(bodyExpr, paramExpr);
but always getting error when trying to compile this:
variable 'x' of type 'MyClass' referenced from scope '', but it is not defined
how i could do this correctly?
The problem here is that you're using lambdaMember.Body, which references the x from x => x.SomeField - but because you only used the .Body, that is undefined - and is unrelated to the x from Expression.Parameter(typeof(MyClass), "x");
In the general case, there are 2 options here:
invoke the entire lambda (i.e. lambdaMember, not lambdaMember.Body) - passing in the arguments to use for the parameters
rewrite the inner lambda at runtime using ExpressionVisitor - swapping out instances of the x from the inner expression with whatever you wanted to use as the argument - presumably paramExpr
The first option is easier, and is just Expression.Invoke:
var bodyExpr = Expression.LessThanOrEqual(
Expression.Call(Expression.Invoke(lambdaMember, paramExpr),
"CompareTo", null, valueExpr), Expression.Constant(0));
Note: there is a third option in this case, since it is a relatively simple example - you can just hijack the parameter from the inner expression and use it instead of declaring paramExpr as a new parameter expression:
var paramExpr = lambdaMember.Parameters.Single();
Expression valueExpr = Expression.Constant(someValue);
var bodyExpr = Expression.LessThanOrEqual(
Expression.Call(lambdaMember.Body,
"CompareTo", null, valueExpr), Expression.Constant(0));
return Expression.Lambda<Func<MyClass, FieldType>>(bodyExpr, lambdaMember.Parameters);
Related
My ultimate goal is to iterate through nested properties in a lambda expression and determine if any of the properties are null, but I am having trouble creating a new lambda expression based on a member expression.
Take this dummy method:
public static void DoStuff<TModelDetail, TValue>(Expression<Func<TModelDetail, TValue>> expr, TModelDetail detail)
{
var memberExpression = expr.Body as MemberExpression;
if (memberExpression == null && expr.Body is UnaryExpression)
{
memberExpression = ((UnaryExpression)expr.Body).Operand as MemberExpression;
}
var pe = Expression.Parameter(typeof(TModelDetail), "x");
var convert = Expression.Convert(memberExpression, typeof(object));
var wee = Expression.Lambda<Func<TModelDetail, object>>(convert, pe);
var hey = wee.Compile()(detail);
}
On the Compile.exec line I get the following error:
variable 'x' of type 'Blah' referenced from scope '', but it is not defined
where Blah is the type of TModelDetail.
How do I build the lambda with the MemberExpression? What I ultimately want to do is recursively find the root member expression, determine if it is null, and bubble up and determine if each subsequent member expression is null.
expr already contains a parameter (let's call it y) which is bound by your member expression, so expr looks like y => y.Member.Something.
When your construct the new lambda Expression wee you are giving it a new parameter x, so wee looks like x => y.Member, which doesn’t make sense.
Therefore you need to reuse the parameter from expr for wee.
I have a scenario where I have two tables 'side' and 'item'.
I'm trying to create the following query:
_db.SideTable.Where(s => s.Active && !_db.ItemTable.Any(i => i.SIDE_1 == s.ID));
But where SIDE_1 is actually dynamic (SIDE_1 through SIDE_20) where the number 1-20 comes from a variable 'sideId'.
I've tried creating doing the following:
// Access the parameter's SIDE_xxx property
ParameterExpression param = Expression.Parameter(typeof(ItemTable));
Expression property = Expression.Property(param, "SIDE_" + sideId);
// Access the outer variable's ID property
ParameterExpression var = Expression.Variable(typeof(SideTable), "s");
Expression property2 = Expression.Property(var, "ID");
// Make the ID property value int? since the SIDE_xxx property may be null and otherwise the expression will fail
var valExpr = Expression.Convert(property2, typeof(int?));
// The body of the expression is simply an equal check
Expression body = Expression.Equal(property, valExpr);
// Create the expression
var expression = Expression.Lambda<Func<Artikel, bool>>(body, param);
// Get the data
_db.SideTable.Where(s => s.Active && !_db.ItemTable.Any(expression)).ToList();
When I run this however I'm greeted with the following exception:
The parameter 's' was not bound in the specified LINQ to Entities query expression
I guess I'm not using the right syntax to access the outer 's' property correctly, but I have no idea how to do it otherwise.
I'm working with expressions and I have the following expression
//p=> 5 == p % 5
var p = Expression.Parameter(typeof(int), "p");
var e = Expression.MakeBinary(ExpressionType.Equal,
Expression.Constant(5),
Expression.Modulo(
p,
Expression.Constant(5)
));
var lambda = Expression.Lambda(e, Expression.Parameter(typeof(int), "p"));
var func = lambda.Compile();
I'm getting the current exception:
An unexpected exception type was thrown
Expected: System.NotImplementedException
but was: System.InvalidOperationException : variable 'p' of type 'System.Int32' referenced from scope '', but it is not defined
Now this makes sense but what I find confusing is that when the Lambda expression is compiled does the Expression "compiler" keep reference of all variables in it's stack not the names?
Which means it could be possible to build a lambda expression
(p,p)=>p % p == 5
where each p is different?
How is this information stored at runtime?
Yes, two parameters created with the same type and name are different.
Change the line
var lambda = Expression.Lambda(e, Expression.Parameter(typeof(int), "p"));
to
var lambda = Expression.Lambda(e, p);
I'm using System.Linq.Expressions
I was attempting to build a simple LambdaExpression that includes a MemberExpression. If I create the MemberExpression explicitly with the System.Linq.Expressions API (e.g. MakeMemberAccess), I will get the error "InvalidOperationExpression variable 'x' referenced from scope '', but it is not defined" when I call Compile() on the LambdaExpression.
For example ,this is my code
Expression<Func<Customer, string>> expression1, expression2, expression3;
Func<Customer, string> fn;
expression1 = (x) => x.Title;
fn = expression1.Compile();//works
fn(c);
MemberExpression m;
m = Expression.MakeMemberAccess(
Expression.Parameter(typeof(Customer), "x"), typeof(Customer).GetProperty("Title"));
expression2 = Expression.Lambda<Func<Customer, string>>(m,
Expression.Parameter(typeof(Customer), "x"));
m = Expression.Property(Expression.Parameter(typeof(Customer),"x"), "Title");
expression3 = Expression.Lambda<Func<Customer, string>>(m,
Expression.Parameter(typeof(Customer), "x"));
fn = expression3.Compile();//InvalidOperationExpression variable 'x' referenced from scope '', but it is not defined
fn = expression2.Compile();//InvalidOperationExpression variable 'x' referenced from scope '', but it is not defined
expression2 and expression3 throw an exception when the Compile() method is called, but expression1 does not; expression1 works. Why is this? How do I create an MemberExpression like in expressions 2, 3 and get them to work (not throw an exception) when I call Compile()?
Thanks
You're creating different parameters called "x" several times. If you use a single ParameterExpression, it should all work fine.
ParameterExpression p = Expression.Parameter(typeof(Customer), "x");
MemberExpression m = Expression.MakeMemberAccess(p,
typeof(Customer).GetProperty("Title"));
expression2 = Expression.Lambda<Func<Customer, string>>(m, p);
m = Expression.Property(p, "Title");
expression3 = Expression.Lambda<Func<Customer, string>>(m, p);
fn = expression3.Compile();
fn = expression2.Compile();
Basically parameter expressions aren't matched by name - you've got to use the same one everywhere. It's a bit of a pain, but there we go...
This is the code I need to alter:
var xParam = Expression.Parameter(typeof(E), typeof(E).Name);
MemberExpression leftExpr = MemberExpression.Property(xParam, this._KeyProperty);
Expression rightExpr = Expression.Constant(id);
BinaryExpression binaryExpr = MemberExpression.Equal(leftExpr, rightExpr);
//Create Lambda Expression for the selection
Expression<Func<E, bool>> lambdaExpr = Expression.Lambda<Func<E, bool>>(binaryExpr, new ParameterExpression[] { xParam });
Right now the expression I'm getting out of this is (x => x.RowId == id) and what I want to change it to is (x => x.RowId) so that I can use it in an OrderBy for the ObjectContext.CreateQuery(T) method called later on.
Does anyone know how to change the above code so the lambda is correct to use in an OrderBy to order by the ID field?
Side Notes: The RowId is coming from this._KeyProperty I believe. This is part of a generic repository using the entity framework on Asp.Net MVC
Just omit creating the constant and "=":
var xParam = Expression.Parameter(typeof(E), "x");
var propertyAccessExpr = Expression.Property(xParam, this._KeyProperty);
var lambdaExpr = Expression.Lambda<Func<E, bool>>(propertyAccessExpr, xParam);
This assumes that _KeyProperty has type 'bool'. If it has a different type, just change Func<E, bool> to the appropriate type.
(Edited to incorporate asgerhallas and LukLed's good suggestions)