At your request, all fields should have the same type, at least in static typed C #.
The Queriable.Where method receives the predicate Expression<Func<T, bool>> as a parameter. Therefore, you need to build the predicate o.p1 == val || o.p2 == val || o.p3 = val ... o.p1 == val || o.p2 == val || o.p3 = val ... o.p1 == val || o.p2 == val || o.p3 = val ... like Expression . Here o is the parameter Expression<Func<T, bool>> :
public Expression BuildExpression<TObj, TVal>(TObj obj, TVal val) { Expression<Func<TObj, bool>> predicate = (o) => o.p1 == val || ... || o.pN == val; return predicate; }
but we need to construct the predicate dynamically for all TObj properties of type TVal .
To simplify the code, we construct an equal expression false || o.p1 == val || ... || o.pN == val false || o.p1 == val || ... || o.pN == val false || o.p1 == val || ... || o.pN == val .
public Expression<Func<TObj, bool>> BuildExpression<TObj, TVal>(TVal val) { var parameter = Expression.Parameter(typeof(TObj), "o"); var valExpression = Expression.Constant(val, typeof(TVal)); var body = Expression.Constant(false, typeof(bool)); var properties = typeof(TObj).GetProperties() .Where(p => p.PropertyType == typeof(TVal)); foreach (var property in properties) { var propertyExpression = Expression.Property(parameter, property); var equalExpression = Expression.Equal(propertyExpression, valExpression); body = Expression.Or(body, equalExpression); } return Expression.Lambda<Func<TObj, bool>>(body, parameter); } . . . using (var dbContext = new DbContext()) { var whereExpression = BuildExpression<User, string>("foo"); var contaningsFoo = dbContext.Users.Where(whereExpression); }
source share