Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tornikegomareli/55a636ec6ea34fcf79f266713ab7960f to your computer and use it in GitHub Desktop.
Save tornikegomareli/55a636ec6ea34fcf79f266713ab7960f to your computer and use it in GitHub Desktop.
public static Expression<Func<T, bool>> StrToFunc<T>(string propName, string opr, string value, Expression<Func<T, bool>> expr = null)
{
Expression<Func<T, bool>> func = null;
try
{
var type = typeof(T);
var prop = type.GetProperty(propName);
ParameterExpression tpe = Expression.Parameter(typeof(T));
Expression left = Expression.Property(tpe, prop);
Expression right = Expression.Convert(ToExprConstant(prop, value), prop.PropertyType);
Expression<Func<T, bool>> innerExpr = Expression.Lambda<Func<T, bool>>(ApplyFilter(opr, left, right), tpe);
if (expr != null)
innerExpr = innerExpr.And(expr);
func = innerExpr;
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
return func;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment