我试图通过创建一个扩展方法来一般地处理过滤来清理我的代码。
这是我想要清理的代码。
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);
这显然非常重复。所以我尝试创建一个使用反射按属性过滤的扩展方法。这是方法。
public static void FilterByValue<T>(this IQueryable<T> 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<Func<T, string>>
作为参数,以便您具有编译时支持以验证所选对象是否有效。
我们将从表示通用部分的表达式开始;它将表示具有* two *参数的函数,对象和给定属性的值。然后,我们可以使用我们在实际方法的参数中定义的属性选择器替换第二个参数的所有实例。
public static IQueryable<T> FilterByValue<T>(
this IQueryable<T> obj,
Expression<Func<T, string>> propertySelector,
string propertyValue)
{
if (!string.IsNullOrEmpty(propertyValue))
{
Expression<Func<T, string, bool>> 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<Func<T, bool>>(
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);
从技术上讲,是的,你可以做到,但你需要构建 Expression
你要传递给自己 Where
。
也就是说,不应该接受属性作为字符串值,而应该考虑接受 Expression<Func<T, string>>
作为参数,以便您具有编译时支持以验证所选对象是否有效。
我们将从表示通用部分的表达式开始;它将表示具有* two *参数的函数,对象和给定属性的值。然后,我们可以使用我们在实际方法的参数中定义的属性选择器替换第二个参数的所有实例。
public static IQueryable<T> FilterByValue<T>(
this IQueryable<T> obj,
Expression<Func<T, string>> propertySelector,
string propertyValue)
{
if (!string.IsNullOrEmpty(propertyValue))
{
Expression<Func<T, string, bool>> 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<Func<T, bool>>(
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);