是否可以将linq的reflection用于实体?

我试图通过创建一个扩展方法来一般地处理过滤来清理我的代码。

这是我想要清理的代码。

var queryResult = (from r in dc.Retailers select r); if (!string.IsNullOrEmpty(firstName)) queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(firstName.Trim(), ex.FirstName.Trim()) > 0); if (!string.IsNullOrEmpty(lastName)) queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(lastName.Trim(), ex.LastName.Trim()) > 0); if (!string.IsNullOrEmpty(companyName)) queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(companyName.Trim(), ex.CompanyName.Trim()) > 0); if (!string.IsNullOrEmpty(phone)) queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(phone.Trim(), ex.Phone.Trim()) > 0); if (!string.IsNullOrEmpty(email)) queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(email.Trim(), ex.Email.Trim()) > 0); if (!string.IsNullOrEmpty(city)) queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(city.Trim(), ex.City.Trim()) > 0); if (!string.IsNullOrEmpty(zip)) queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(zip.Trim(), ex.Zip.Trim()) > 0); if (!string.IsNullOrEmpty(country)) queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(country.Trim(), ex.Country.Trim()) > 0); if (!string.IsNullOrEmpty(state)) queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(state.Trim(), ex.State.Trim()) > 0); 

这显然非常重复。 所以我尝试创建一个使用reflection按属性过滤的扩展方法。 这是方法。

 public static void FilterByValue(this IQueryable obj, string propertyName, string propertyValue) { if (!string.IsNullOrEmpty(propertyValue)) { obj = obj.Where( ex => SqlFunctions.PatIndex(propertyValue.Trim(), (string)typeof(T).GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase).GetValue(ex)) > 0 ); } } 

并且它被称为如此:

 var queryResult = (from r in dc.Retailers select r); queryResult.FilterByValue("firstname", firstName); 

但是,当linq执行时我收到一个错误,指出linq中没有识别出“GetValue”到实体。

那么,有没有其他方法可以清理它,还是我不得不让它变丑?

从技术上讲,是的,你可以做到,但你需要自己构建Expression以传递给Where

也就是说,不应该将属性作为字符串值接受,而应该考虑接受Expression>作为参数,以便您有编译时支持来validation所选对象是否有效。

我们将从表示通用部分的表达式开始; 它将表示具有* two *参数的函数,对象和给定属性的值。 然后,我们可以使用我们在实际方法的参数中定义的属性选择器替换第二个参数的所有实例。

 public static IQueryable FilterByValue( this IQueryable obj, Expression> propertySelector, string propertyValue) { if (!string.IsNullOrEmpty(propertyValue)) { Expression> expression = (ex, value) => SqlFunctions.PatIndex(propertyValue.Trim(), value.Trim()) > 0; var newSelector = propertySelector.Body.Replace( propertySelector.Parameters[0], expression.Parameters[0]); var body = expression.Body.Replace(expression.Parameters[1], newSelector); var lambda = Expression.Lambda>( body, expression.Parameters[0]); return obj.Where(lambda); } else return obj; } 

此方法使用一个函数将一个表达式的所有实例替换为给定表达式中的另一个表达式。 其实施是:

 public class ReplaceVisitor : ExpressionVisitor { private readonly Expression from, to; public ReplaceVisitor(Expression from, Expression to) { this.from = from; this.to = to; } public override Expression Visit(Expression node) { return node == from ? to : base.Visit(node); } } public static Expression Replace(this Expression expression, Expression searchEx, Expression replaceEx) { return new ReplaceVisitor(searchEx, replaceEx).Visit(expression); } 

如果您真的想要将属性名称作为字符串接受,那么只需使用以下内容替换newSelector的定义:

 var newSelector = Expression.Property(expression.Parameters[0], propertyName);