If you need just &&
, then it's helpful to realize that coll.Where(x => a(x) && b(x))
(where a(x)
and b(x)
are any boolean expressions that work with x
) is logically the same as coll.Where(x => a(x)).Where(x => b(x))
. What this means is that you can rewrite your code to something like:
List<Tbl1Type> GetTbl1Values(Expression<Func<Tbl1Type, bool>> whereClause)
{
using (SampleDbEntities dbo = new SampleDbEntities())
{
return dbo.Tbl1
.Where(x => x.Id == (int)comboBoxPerson.SelectedValue)
.Where(whereClause)
.Take(1000).ToList();
}
}
If you also needed to support ||
or more complicated combinations, you could use LINQKit.
This just leaves the matter of creating the expression from a property name. You can use method of the Expression
type for that. For example, something like:
static Expression<Func<T, bool>> CreateWhereClause<T>(
string propertyName, object propertyValue)
{
var parameter = Expression.Parameter(typeof(T));
return Expression.Lambda<Func<T, bool>>(
Expression.Equal(
Expression.Property(parameter, propertyName),
Expression.Constant(propertyValue)),
parameter);
}