- Home
- LLBLGen Pro
- LLBLGen Pro Runtime Framework
Simple Binary Expression Creator
Posts
Posts: 321
Joined: 26-Aug-2006
Joined: 26-Aug-2006
# Posted on: 27-Jun-2010 08:13:09
Hello, I made this expression creator to more easily allow support for creating a where clause using a variety of operators in a convenient way. Havent actually tested all the operators out yet, just equals and Contains and StartsWith. It needs support for a "Contains" for an array (an "IN" query) which i think is a bit of different pattern to what I have done so far. Contains at the moment only works for strings (a LIKE query). This is my first crack at something like this with expressions so I thought I'd share.
Theres nothing really LLBL centric about this code. It should work for any object type.
example use
list = list.Where(ServiceQuery<UserEntity>.ApplyOperator(p => p.FirstName, QueryOperator.StartsWith, "sam"));
public enum QueryOperator
{
None = 0,
Equals,
NotEquals,
LessThan,
GreaterThan,
LessThanEqualTo,
GreaterThanEqualTo,
Contains,
StartsWith
}
(contained in a class with a Type parameter TEntity)
public static System.Linq.Expressions.Expression<Func<TEntity, bool>> ApplyOperator(System.Linq.Expressions.Expression<Func<TEntity, TProperty>> property, QueryOperator op, object argument)
{
return ApplyOperator(((MemberExpression)property.Body).Member.Name, op, argument);
}
public static System.Linq.Expressions.Expression<Func<TEntity, bool>> ApplyOperator(string propertyName, QueryOperator op, object argument)
{
System.Linq.Expressions.Expression<Func<TEntity, bool>> clause;
System.Reflection.MethodInfo mi;
var param = System.Linq.Expressions.Expression.Parameter(typeof(TEntity), "p");
var property = System.Linq.Expressions.Expression.Property(param, propertyName);
switch (op)
{
case QueryOperator.None :
throw new InvalidOperationException("No operator to apply.");
case QueryOperator.GreaterThan :
clause = System.Linq.Expressions.Expression.Lambda<Func<TEntity, bool>>(
LambdaExpression.GreaterThan(property, System.Linq.Expressions.Expression.Constant(argument, argument == null ? typeof(object) : argument.GetType())),
param);
break;
case QueryOperator.GreaterThanEqualTo:
clause = System.Linq.Expressions.Expression.Lambda<Func<TEntity, bool>>(
LambdaExpression.GreaterThanOrEqual(property, System.Linq.Expressions.Expression.Constant(argument, argument == null ? typeof(object) : argument.GetType())),
param);
break;
case QueryOperator.LessThan :
clause = System.Linq.Expressions.Expression.Lambda<Func<TEntity, bool>>(
LambdaExpression.LessThan(property, System.Linq.Expressions.Expression.Constant(argument, argument == null ? typeof(object) : argument.GetType())),
param);
break;
case QueryOperator.LessThanEqualTo:
clause = System.Linq.Expressions.Expression.Lambda<Func<TEntity, bool>>(
LambdaExpression.LessThanOrEqual(property, System.Linq.Expressions.Expression.Constant(argument, argument == null ? typeof(object) : argument.GetType())),
param);
break;
case QueryOperator.Contains :
mi = typeof(string).GetMethod("Contains", new Type[] { typeof(string) }, null);
clause = System.Linq.Expressions.Expression.Lambda<Func<TEntity, bool>>(LambdaExpression.Call(property, mi, System.Linq.Expressions.Expression.Constant(argument, typeof(string))), param);
break;
case QueryOperator.StartsWith :
mi = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) }, null);
clause = System.Linq.Expressions.Expression.Lambda<Func<TEntity, bool>>(LambdaExpression.Call(property, mi, System.Linq.Expressions.Expression.Constant(argument, typeof(string))), param);
break;
case QueryOperator.NotEquals :
clause = System.Linq.Expressions.Expression.Lambda<Func<TEntity, bool>>(
LambdaExpression.NotEqual(property, System.Linq.Expressions.Expression.Constant(argument, argument == null ? typeof(object) : argument.GetType())),
param);
break;
case QueryOperator.Equals :
default :
clause = System.Linq.Expressions.Expression.Lambda<Func<TEntity, bool>>(
LambdaExpression.Equal(property, System.Linq.Expressions.Expression.Constant(argument, argument == null ? typeof(object) : argument.GetType())),
param);
break;
}
return clause;
}