This is an almost-duplicate question of "How do I create an expression tree to represent 'String.Contains("term")' in C#?."
Marc's answer provides this bit of code to build an expression, which is what you are looking for:
static Expression<Func<T, bool>> GetExpression<T>(string propertyName, string propertyValue)
{
var parameterExp = Expression.Parameter(typeof(T), "type");
var propertyExp = Expression.Property(parameterExp, propertyName);
MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
var someValue = Expression.Constant(propertyValue, typeof(string));
var containsMethodExp = Expression.Call(propertyExp, method, someValue);
return Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp);
}
...and now using it from your method:
public static List<Client> GetClientsByFilter(string search, string propertyName)
{
//it's Entity Framework context
using (var dbContext = new LibDbContext())
{
List<Client> clients;
clients = dbContext.Clients
.Where(GetExpression<Client>(propertyName, search)) // Now using Marc's method
.ToList();
}
return clients;
}
Since "GetExpression" is generic, you can easily reuse this for other types besides just Client
. Consider renaming the method since "GetExpression" doesn't spell out your intent - maybe something like "GetPropertyContainsExpression". You might also consider adding some error handling to that method in case the value in propertyName is not a valid property on the type and when the property specified isn't of type string
which probably wouldn't have a Contains
method that takes a string. These sorts of errors might be hard to figure out from the exceptions the expression builder might throw at run time.