1
我有这样的:从属性名作为字符串List.Contains(x.Property)
// selectedOptions Contains a list of string constants
var selectedOptions = new List<string>(...);
Expression<Func<T, bool>> condition = null;
switch (propertyName)
{
case "Property1":
condition = x => selectedOptions.Contains(x.Property1);
break;
case "Property1":
condition = x => selectedOptions.Contains(x.Property2);
break;
case "Property3":
condition = x => selectedOptions.Contains(x.Property3);
break;
}
的条件是在LINQ to被用作谓语为凡()来实体。 这个想法是为EF生成SQL沿where Property1 in (...)
的东西。 我不知道是否有更好的方法来做到这一点,但它的工作原理。
我的问题是,我想消除开关,并具有沿着线的东西:
condition = x => selectedOptions.Contains(x.[propertyName]);
这可能吗?
您需要使用'Expression'类手动构建表达式树。 – SLaks