I want to create lambda something like this
user => user.Address == address
but is not compiled one, I want to return LambdaExpression
If the lambda take constant like this
user => user.Age == 50
Then I can use this method
public static LambdaExpression PropertyEqual(Type tEntityType, string propertyName, object value)
{
// entity => entity.PropName == const
var itemParameter = Expression.Parameter(tEntityType, "entity");
return Expression.Lambda
(
Expression.Equal
(
Expression.Property
(
itemParameter,
propertyName
),
Expression.Constant(value) // Tried to replace this with Expression.Parameter or Expression.Variable but no luck
),
new[] { itemParameter }
);
}
How to make this method accept variable address
come from the scope just outside from the lambda expression?
var addressPropertyName = "Address";
var address = new Address() {...};
var q = Repo.GetQuery().Where(PropertyEqual(typeof(User), addressPropertyName, address))
Edit: clarify my question: How build the right Expression
to generate the first lambda?
Update: This is not possible because EF does not support non-scalar variable
I change the lambda to user => user.AddressId == addressId
as suggested here. It just the matter how to get AddressId
FK PropertyInfo
from a known navigation property Address
.