I am trying to create a method that can accept multiple property names and a string value that will be use to research in entity possessing those properties
I want to a generic method to replace this one:
public static Client[] GetClientByInfo(string info)
{
return Context.Db.Clients.Where(c =>
c.LastName.Contains(info) ||
c.FirstName.Contains(info)).ToArray();
}
So I tried this:
public static T[] FindByText<T>(string text, string[] properties)
where T: class
{
return Context.Db.Set<T>().AsNoTracking().Where(PropertyEquals<T, string>(properties, text)).ToArray();
}
public static Expression<Func<TItem, bool>> PropertyEquals<TItem, TValue>(string[] properties, TValue value)
{
MethodInfo startWithMethod = typeof(string).GetMethod("StartsWith", BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(string) }, null);
ParameterExpression parameter = Expression.Parameter(typeof(TItem));
ConstantExpression constant = Expression.Constant(value, typeof(TValue));
MemberExpression[] members = new MemberExpression[properties.Length];
for (int i = 0; i < properties.Length; i++)
members[i] = Expression.Property(parameter, properties[i]);
MethodCallExpression callExp = Expression.Call(parameter, startWithMethod, members);
return Expression.Lambda<Func<TItem, bool>>(callExp, parameter);
}
I do something wrong when I call Expression.Call Is it possible to build an expression like this or i must use concatenate expression?
TValue value
parameter inPropertyEquals
? – Shlomo Dec 10 '15 at 4:42