Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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));
case "startswith":
MethodInfo startsWithMethodInfo = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });
comparingExpression = Expression.Call(member, startsWithMethodInfo, Expression.Convert(constant, member.Type));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment