Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hbulens/f01cc47253ebc35b67ca3956f8be767f to your computer and use it in GitHub Desktop.
Save hbulens/f01cc47253ebc35b67ca3956f8be767f to your computer and use it in GitHub Desktop.
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