Last active
January 21, 2018 20:20
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Expression<Func<T, bool>> GetExpressionForType<TEntity>(string field, string operation, object value, string ignoreCase) | |
{ | |
Expression<Func<T, bool>> whereClause = default(Expression<Func<T, bool>>); | |
ParameterExpression param = Expression.Parameter(typeof(T), "x"); | |
MemberExpression member = Expression.Property(param, field); | |
Type propertyType = member.Type; | |
TypeConverter converter = TypeDescriptor.GetConverter(propertyType); | |
var result = converter.ConvertFrom(value.ToString()); | |
ConstantExpression constant = Expression.Constant(result); | |
Expression comparingExpression = default(BinaryExpression); | |
switch (operation) | |
{ | |
case "eq": | |
comparingExpression = Expression.Equal(member, Expression.Convert(constant, member.Type)); | |
break; | |
case "startswith": | |
MethodInfo startsWithMethodInfo = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) }); | |
comparingExpression = Expression.Call(member, startsWithMethodInfo, Expression.Convert(constant, member.Type)); | |
break; | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment