Below expression compares property NAME with the value PETER.
ParameterExpression pe = Expression.Parameter(typeof(T), "x");
MemberExpression member = Expression.Property(pe, "name");
ConstantExpression value = Expression.Constant("Peter");
exp = Expression.Equal(member, value);
What if the property is a class:
public class Address
{
public string Name {get; set;}
}
Then the expression would look something similar to this:
MemberExpression member = Expression.Property(pe, "Address.Name");
ConstantExpression value = Expression.Constant("Peter");
exp = Expression.Equal(member, value);
This would fail because the member type doesn't match the value type.
So, the question is: How to build an Expression that would work using the above class sample ??
I use this expression in a NHibernate.Linq query:
var q = from f in data //of type IQueryable<T>
select f;
if (filter != null) //filter of type Expression<Func<T, bool>>
q = q.Where(filter);
etc....
Thank you.
UPDATE by Peter:
Based on the code from xanatos (next post) I have created the following test to understand how it works. Its not very different from what xanatos do, but at first I could not get it to work, so I decided to write it allover in one simple test, and that did it. With thanks to xanatos:
[Test]
public void FilterWithDeepProperties()
{
//Arrange
IGenericGridRepository repository = ObjectFactory.GetInstance<IGenericGridRepository>();
FilterDescriptor filter = new FilterDescriptor("AgreementId.Name", FilterOperator.IsEqualTo, "a name");
string[] properties = filter.Member.Split('.');
ParameterExpression pe = Expression.Parameter(typeof(SampleDomain), "x");
//Act
Expression lastMember = pe;
for (int i = 0; i < properties.Length; i++)
{
MemberExpression member = Expression.Property(lastMember, properties[i]);
lastMember = member;
}
ConstantExpression valueExpression = Expression.Constant(filter.Value);
Expression equalityExpression = Expression.Equal(lastMember, valueExpression);
Expression<Func<SampleDomain, bool>> where = Expression.Lambda<Func<SampleDomain, bool>>(equalityExpression, pe);
var result = repository.GetObjects<SampleDomain>(filter: where);
//Assert
result.Count().Should().BeGreaterThan(0, "because there are many schedule items equals to " + filter.Value);
}
You probably want something like:
public static Expression<Func<TSource, bool>> GetEquality<TSource>(object value, params string[] properties)
{
ParameterExpression pe = Expression.Parameter(typeof(TSource), "source");
Expression lastMember = pe;
for (int i = 0; i < properties.Length; i++)
{
MemberExpression member = Expression.Property(lastMember, properties[i]);
lastMember = member;
}
Expression valueExpression = Expression.Constant(value);
Expression equalityExpression = Expression.Equal(lastMember, valueExpression);
Expression<Func<TSource, bool>> lambda = Expression.Lambda<Func<TSource, bool>>(equalityExpression, pe);
return lambda;
}
Use it like:
Expression exp = GetEquality<Person>("Foo", "Address", "Name");
Where Foo is your Peter (so the value that must be compared), while Address and Name are the names of the "chain" of properties. For example I'm using
public class Person
{
public Address Address { get; set; }
}
public class Address
{
public string Name { get; set; }
}
So the expression generated is
source.Address.Name == "Foo"
If you want to use something like Address.Name, you can use the method like
Expression exp = GetEquality<Person>("Foo", "Address.Name".Split('.'));
Related
I'm using the following approach to get the name of a property specified in a lambda by inspecting the resulting ExpressionTree:
var name =GetPropertyName<Entity1, Entity2>(x => x.Entity2);
public string GetPropertyName<T, P>(Expression<Func<T, P>> expression)
{
var memberExpression = expression.Body as MemberExpression;
var propertyInfo = memberExpression.Member as PropertyInfo;
return propertyInfo.Name;
}
//name is "Entity2"
Given the following expression i want to extract the names "Entity2s", "Entity3s" and "Entity4s"
var names = GetPropertyNames<Entity1, IEnumerable<Entity4>>(x => x.Entity2s.SelectMany(x => x.Entity3s).SelectMany(x => x.Entity4s));
With an ExpressionVisitor it is a simple as
public static List<string> GetPropertyNames<T, P>(Expression<Func<T, P>> expression)
{
var visitor = new Visitor();
visitor.Visit(expression);
return visitor.Names;
}
class Visitor : ExpressionVisitor
{
public Visitor()
{
Names = new List<string>();
}
public List<string> Names { get; }
protected override Expression VisitMember(MemberExpression node)
{
Names.Add(node.Member.Name);
return base.VisitMember(node);
}
}
Here is a method for extracting the member names from exactly the Expression tree you provided. A more generic method should probably be based on an ExpressionVisitor subclass.
public static List<string> GetPropertyNames<T, P>(Expression<Func<T, P>> fe) {
var ans = new List<string>();
var callExpr1 = fe.Body as MethodCallExpression;
var callExpr2 = callExpr1.Arguments[0] as MethodCallExpression;
var e2MemberExpr1 = callExpr2.Arguments[0] as MemberExpression;
ans.Add(e2MemberExpr1.Member.Name);
var e2MemberExpr2 = (callExpr2.Arguments[1] as LambdaExpression).Body as MemberExpression;
ans.Add(e2MemberExpr2.Member.Name);
var e1MemberExpr1 = (callExpr1.Arguments[1] as LambdaExpression).Body as MemberExpression;
ans.Add(e1MemberExpr1.Member.Name);
return ans;
}
I'm trying to build a dynamic search on nested objects, which will later be sent to EF and SQL Server. So far, I'm able to search on all properties of the first object. Here's a very simplified version:
public class User
{
public string Name { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string City { get; set; }
}
public class MyClass<TEntity> where TEntity : class {
public IQueryable<TEntity> applySearch(IQueryable<TEntity> originalList, string propName, string valueToSearch) {
ParameterExpression parameterExpression = Expression.Parameter(typeof(TEntity), "p");
PropertyInfo propertyInfo = typeof(TEntity).GetProperty(propName);
MemberExpression member = Expression.MakeMemberAccess(parameterExpression, propertyInfo);
lambda = Expression.Lambda<Func<TEntity, bool>>(Expression.Equal(member, Expression.Constant(valueToSearch)), parameterExpression);
return originalList.Where(expression);
}
}
When propName = "Name" everything is fine, but when propName = "Address.City", the propertyInfo is null, and I get this error on the member assignment line:
System.ArgumentNullException: Value cannot be null
I was able to obtain the propertyInfo of the nested property using the solution from this answer:
PropertyInfo propertyInfo = GetPropertyRecursive(typeof(TEntity), propName);
...
private PropertyInfo GetPropertyRecursive(Type baseType, string propertyName)
{
string[] parts = propertyName.Split('.');
return (parts.Length > 1)
? GetPropertyRecursive(baseType.GetProperty(parts[0]).PropertyType, parts.Skip(1).Aggregate((a, i) => a + "." + i))
: baseType.GetProperty(propertyName);
}
But then I get this error on member assignment:
System.ArgumentException: Property 'System.String City' is not defined for type 'User'
This should point to Address instead of User, but I don't know if I'm on right track here, I mean, should I change parameterExpression now?
How can I make a dynamic search on nested objects, so that this can be turned into a lambda expression and later sent to SQL?
After Kobi's advice and a lot of trial and error, I finally got this working. This uses the Universal PredicateBuilder. Here it is:
public class MyClass<TEntity> where TEntity : class
{
public IQueryable<TEntity> ApplySearch(IQueryable<TEntity> originalList, string valueToSearch, string[] columnsToSearch)
{
Expression<Func<TEntity, bool>> expression = null;
foreach (var propName in columnsToSearch)
{
Expression<Func<TEntity, bool>> lambda = null;
ParameterExpression parameterExpression = Expression.Parameter(typeof(TEntity), "p");
string[] nestedProperties = propName.Split('.');
Expression member = parameterExpression;
foreach (string prop in nestedProperties)
{
member = Expression.PropertyOrField(member, prop);
}
var canConvert = CanConvertToType(valueToSearch, member.Type.FullName);
if (canConvert)
{
var value = ConvertToType(valueToSearch, member.Type.FullName);
if (member.Type.Name == "String")
{
ConstantExpression constant = Expression.Constant(value);
MethodInfo mi = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });
Expression call = Expression.Call(member, mi, constant);
lambda = Expression.Lambda<Func<TEntity, bool>>(call, parameterExpression);
}
else
{
lambda = Expression.Lambda<Func<TEntity, bool>>(Expression.Equal(member, Expression.Constant(value)), parameterExpression);
}
}
if (lambda != null)
{
if (expression == null)
{
expression = lambda;
}
else
{
expression = expression.Or(lambda);
}
}
}
if (expression != null)
{
return originalList.Where(expression);
}
return originalList;
}
}
private bool CanConvertToType(object value, string type)
{
bool canConvert;
try
{
var cValue = ConvertToType(value, type);
canConvert = true;
}
catch
{
canConvert = false;
}
return canConvert;
}
private dynamic ConvertToType(object value, string type)
{
return Convert.ChangeType(value, Type.GetType(type));
}
Warning in advance - I'm not building the expression, just inspecting its structure.
When I need to dynamically create Expressions, I find it useful to inspect an Expression and copy its structure:
Expression<Func<User, string>> getCity = user => user.Address.City;
Now you can simply debug it, for example in the immediate window (ctrlalti here):
getCity
{user => user.Address.City}
Body: {user.Address.City}
CanReduce: false
DebugView: ".Lambda #Lambda1<System.Func`2[ConsoleApplication1.User,System.String]>(ConsoleApplication1.User $user) {\r\n ($user.Address).City\r\n}"
Name: null
NodeType: Lambda
Parameters: Count = 1
ReturnType: {Name = "String" FullName = "System.String"}
TailCall: false
Here we can see getCity is a Lambda with one parameter. Let's inspect it's body:
getCity.Body
{user.Address.City}
CanReduce: false
DebugView: "($user.Address).City"
Expression: {user.Address}
Member: {System.String City}
NodeType: MemberAccess
Type: {Name = "String" FullName = "System.String"}
getCity.Body is a member access - it accesses the member City of the Expression user.Address. Technically that's a PropertyExpression, which is an internal class so we can't even cast to it, but that's OK.
Finally, let's look at that inner expression:
((MemberExpression)getCity.Body).Expression
{user.Address}
CanReduce: false
DebugView: "$user.Address"
Expression: {user}
Member: {ConsoleApplication1.Address Address}
NodeType: MemberAccess
Type: {Name = "Address" FullName = "ConsoleApplication1.Address"}
That's just user.Address.
Now we can build an identical expression:
var addressProperty = typeof (User).GetProperty("Address");
var cityProperty = typeof(Address).GetProperty("City");
var userParameter = Expression.Parameter(typeof (User), "user");
var getCityFromUserParameter = Expression.Property(Expression.Property(userParameter, addressProperty), cityProperty);
var lambdaGetCity = Expression.Lambda<Func<User, string>>(getCityFromUserParameter, userParameter);
Expression.MakeMemberAccess works too, instead of Expression.Property.
Obviously, you'd need to build your expression in a loop, and more dynamically, but the structure is the same.
It might be worth taking a look at Linqkit's predicate builder...
http://www.albahari.com/nutshell/predicatebuilder.aspx
I'd also take a look at Entity SQL...
https://msdn.microsoft.com/en-us/library/vstudio/bb387145(v=vs.100).aspx
You might be reinventing a wheel with the code you're writing.
Also I should comment, in terms of the SQL Server plan caching, unless you have no other choice I wouldn't dynamically build queries. You're better off creating a single query that handles all your cases that SQL Server can cache a plan for, your queries will run a lot slower if every time they're executed no plan is hit in SQL Server's plan cache.
I am dealing with the scanario of Nullable types during formation of dynamic query expressions. These expressions would fetch filtered data from any SQL Tables( interfacing with Code First classes using EF ).
I have normal object ( e.g Consignment operating on several properties along with Nullable properties).
My expression formation goes well untill I encounter some Nullable types. On these nullables, I am getting
The binary operator NotEqual is not defined for the types 'System.Nullable`1[System.Single]' and 'System.Single'.
For removing this exception, I am using all appraoches regarding convertion posted on different threads.
Invoking lambda expressions in Expression trees
Trying to filter on a Nullable type using Expression Trees
These all are generating expressions with added word "Convert" ( i.e Convert(someValue) ) and in result I always have expression
t=>(t.Consignment.Id = 45000 && t.Consignment.someProperty>=45 Or t.Consignment.Weight! = Convert(5000)).
Of course I need the whole above expression WITHOUT "Convert". Because this "Convert" will not fetch the data from tables accordingly.
Any help would be greatly appreciated! What should left to do? I already know conversion, but this makes the whole expression useless, because it won't project the records because of needless "Convert"
Added
Expression NotEqual<T>(Expression PropertyType, ConstantExpression a_Constant, ParameterExpression parameter)
{
if(IsNullableType(Property.Type) &&!IsNullableType(a_Constant.Type))
{
var converted = a_Constant.Type != Property.Type ? (Expression)Expression.Convert(a_Constant, Property.Type): (Expression)a_Constant;
// here above statement returns (Convert(50000)) and all I want (50000), but i tried all combinitions from Expression in order to form this constant as expression, it always throws exception what I mentioned originally.
var body = Expression.MakeBinary(ExpressionType.NotEqual, PropertyType, converted);
//MakeBinary statement returns {(t.Weight != Convert(5000000))} but I need {(t.Weight != 5000000)}
var expr = Expression.Lambda<Func<T, bool>>(body, parameter);
return expr;
}
}
Code:
public class Consignment
{
public float? Weight { get; set; }
}
public static class GenericQueryExpressionBuilder
{
private static Expression NotEqual<T>(Expression memberExpression, ConstantExpression a_Constant, ParameterExpression parameter)
{
ConstantExpression constantExpression = null;
if (IsNullableType(memberExpression.Type) && !IsNullableType(a_Constant.Type))
{
//var converted = a_Constant.Type != memberExpression.Type ? (Expression)Expression.Convert(a_Constant, memberExpression.Type) : (Expression)a_Constant;
Expression constantExp = Expression.Property(a_Constant,typeof(T),"Weight");
**// above statement throws exception I commented.**
var body = Expression.MakeBinary(ExpressionType.NotEqual, memberExpression, converted);
//here I want "t=>(t.Weight!=5000.0) INSTEAD of t=>(t.Weight!=Convert(5000.0))"
var expr = Expression.Lambda<Func<T, bool>>(body, parameter);
return expr;
}
else if (!IsNullableType(memberExpression.Type) && IsNullableType(a_Constant.Type))
memberExpression = Expression.Convert(memberExpression, a_Constant.Type);
return Expression.NotEqual(memberExpression, constantExpression);
}
static bool IsNullableType(Type t)
{
return t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>);
}
private static Expression GetExpression<T>(ParameterExpression param, string a_strPropertyName, string Operator, object Value)
{
MemberExpression member = Expression.Property(param, a_strPropertyName);
ConstantExpression constant = Expression.Constant(Value);
try
{
return GenericQueryExpressionBuilder.NotEqual<T>(member, constant, param);
}
catch (InvalidOperationException)
{
return null;
}
return null;
}
public static Expression<Func<T, bool>> GetExpression<T>(Consignment consignment)
{
Expression expression = null;
var parameter = Expression.Parameter(typeof(T), "t");
string PropertyName = "Weight";
string Operation = "NotEqual";
object Value = consignment.Weight;
expression = GenericQueryExpressionBuilder.GetExpression<T>(parameter, PropertyName, Operation, Value);
return Expression.Lambda<Func<T, bool>>(expression, parameter);
}
}
class Program
{
static void Main(string[] args)
{
Consignment consignment = new Consignment();
consignment.Weight = 50000.0f;
var deleg = GenericQueryExpressionBuilder.GetExpression<Consignment>(consignment).Compile();
}
}
Here's a short but complete example showing how to build the c => c.Weight.HasValue && c.Weight.Value != 5000f expression tree. I've removed a lot of irrelevant code from the question:
using System;
using System.Linq.Expressions;
public class Consignment
{
public float? Weight { get; set; }
}
public class Test
{
private static Expression NotEqual(Expression memberExpression,
ConstantExpression constantToCompare)
{
// Other cases removed, for simplicity. This answer only demonstrates
// how to handle c => c.Weight != 5000f.
var hasValueExpression = Expression.Property(memberExpression, "HasValue");
var valueExpression = Expression.Property(memberExpression, "Value");
var notEqual = Expression.NotEqual(valueExpression, constantToCompare);
return Expression.AndAlso(hasValueExpression, notEqual);
}
static void Main(string[] args)
{
Consignment consignment = new Consignment();
consignment.Weight = 50000.0f;
var parameter = Expression.Parameter(typeof(Consignment), "c");
var weight = Expression.Property(parameter, "Weight");
var constant = Expression.Constant(5000f, typeof(float));
var weightNotEqualExpression = NotEqual(weight, constant);
var lambda = Expression.Lambda<Func<Consignment, bool>>
(weightNotEqualExpression, parameter);
Console.WriteLine(lambda);
}
}
I have 3 classes.
AutoYearMake{
int Year { get; set; }
string Make { get; set; }
}
AutoModel{
IAutoYearMake AutoYearMakeParent { get; set; }
string Model { get; set; }
}
AutoTrim{
IAutoModel AutoModelParent { get; set; }
string Trim { get; set; }
}
I need to create a query to a database. How can I get dynamically an expression like:
Expression<Func<AutoTrim, bool>> expression = expression = t => t.AutoModelParent.AutoYearMakeParent.Year == year.Value
&& t.AutoModelParent.AutoYearMakeParent.Make
== make && t.AutoModelParent.Model == model;
This is my code. It doesn't work.
ParameterExpression parameter = Expression.Parameter(typeof (AutoTrim), "a");
MemberExpression yearProp = Expression.Property(parameter, "AutoModelParent.AutoYearMakeParent.Year");
MemberExpression makeProp= Expression.Property(parameter, "AutoModelParent.AutoYearMakeParent.Make");
MemberExpression modelProp= Expression.Property(parameter, "AutoModelParent.Model");
Expression right = Expression.Constant(2014);
Expression e1 = Expression.Equal(yearProp, right);
right = Expression.Constant("make");
Expression e2 = Expression.Equal(makeProp, right);
right = Expression.Constant("model");
Expression e3 = Expression.Equal(modelProp, right);
Expression predicateBody = Expression.AndAlso(e1, e2);
Expression final = Expression.AndAlso(e1, e2);
How can I resolve this problem? I tried to use Expression.Call. It wasn't right way.
The first argument of the Expression.Property() methods is object from which property should be accessed. If you want to access property AutoModelParent from parameter t you use:
Expression.Property(parameter, "AutoModelParent")
So, if you want to access Model property of AutoModelParent property of parameter t you can go with:
Expression.Property(Expression.Property(parameter, "AutoModelParent"), "Model")
Consider this solution:
private static MemberExpression GetPropertyPathAccessor(Expression parameter, string path)
{
return (MemberExpression) path.Split('.').Aggregate(parameter, Expression.Property);
}
or if you don't like one-liners
private static MemberExpression GetPropertyPathAccessor(Expression parameter, string path)
{
Expression current = parameter;
foreach (var propertyName in path.Split('.'))
{
current = Expression.Property(current, propertyName);
}
return (MemberExpression)current;
}
Then you can use:
MemberExpression yearProp = GetPropertyPathAccessor(parameter, "AutoModelParent.AutoYearMakeParent.Year");
How about this?
After that you have the Func<> in expFunc and you can use it right away.
var argParam = Expression.Parameter(typeof(AutoTrim), "s");
var expFunc = Expression.Lambda<Func<AutoTrim, bool>>(
Expression.AndAlso(
Expression.AndAlso(
Expression.Equal(
Expression.Property(Expression.Property(Expression.Property(argParam, "AutoModelParent"), "AutoYearMakeParent"), "Year"),
Expression.Constant(year.Value)),
Expression.Equal(
Expression.Property(Expression.Property(Expression.Property(argParam, "AutoModelParent"), "AutoYearMakeParent"), "Make"),
Expression.Constant(make))
),
Expression.Equal(
Expression.Property(Expression.Property(argParam, "AutoModelParent"), "Model"),
Expression.Constant(model))
),
argParam
).Compile();
But of course you need these too (just example values):
int? year = 2000;
string make = "BMW";
string model = "6";
Note: I know it's much simple to create this using dynamic linq but I want to learn.
I want to create a lambda that "finds": Name=David AND Age=10.
class Person
{
public int Age { get; set; }
public string Name { get; set; }
}
var lambda = LabmdaExpression<Person>("Name", "David", "Age", 10);
static Expression<Func<T, bool>> LabmdaExpression<T>(string property1, string value1, string property2, int value2)
{
ParameterExpression parameterExpression = Expression.Parameter(typeof(Person), "o");
MemberExpression memberExpression1 = Expression.PropertyOrField(parameterExpression, property1);
MemberExpression memberExpression2 = Expression.PropertyOrField(parameterExpression, property2);
ConstantExpression valueExpression1 = Expression.Constant(value1, typeof(string));
ConstantExpression valueExpression2 = Expression.Constant(value2, typeof(int));
BinaryExpression binaryExpression1 = Expression.Equal(memberExpression1, valueExpression1);
BinaryExpression binaryExpression2 = Expression.Equal(memberExpression2, valueExpression2);
var ret1 = Expression.Lambda<Func<T, bool>>(binaryExpression1, parameterExpression);
var ret2 = Expression.Lambda<Func<T, bool>>(binaryExpression2, parameterExpression);
}
Expression andExpression = Expression.AndAlso(binaryExpression1, binaryExpression2);
return Expression.Lambda<Func<T, bool>>(andExpression , parameterExpression);
Edit - comment
You just chain together all your expresions
so in order to get this expression
X AND (Y OR (Z OR Q))
Expression ZorQ = Expression.OrElse(zExp, qExp);
Expression YorZorQ = Expression.OrElse(yExp, ZorQ);
Expression XandYorZorQ = Expression.AndAlso(xExp, YorZorQ);