【发布时间】:2021-05-04 06:42:41
【问题描述】:
网站上有一个过滤器,用于通过公共属性查找值: ? propertyName = SomeName & propertyValue = SomeValue;
现在我想知道,例如,如果输入了此属性 (SomENAme1) 的错误名称会怎样 -> Web 应用程序仍会调用 SomeName 属性的过滤吗?即无论大小写都无所谓,输入的词至少与过滤器属性完全匹配?
public static IQueryable<T> ApplyFiltering<T>(this IQueryable<T> source, string propertyLabel, string propertyValue)
{
if (string.IsNullOrEmpty(propertyLabel))
{
return source;
}
var propertyNames = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public)
.Where(property => property.PropertyType == typeof(string) && property.Name == propertyLabel)
.Select(property => property.Name);
//var predicate = PredicateBuilder.New<T>();
Expression<Func<T, bool>> predicate = item => false;
foreach (var name in propertyNames)
{
// if (propertyLabel.Contains(name))
// {
//propertyLabel = name;
//}
predicate = predicate.Or(GetExpression<T>(name, propertyValue));
}
return source.Where(predicate);
}
我已经在 foreach 循环中尝试过:
if (propertyLabel.ToLower().Contains(name.ToLower()))
{
propertyLabel = name;
}
但它不起作用,即使只是小写...
【问题讨论】:
-
在您的 LINQ 的
Where中,您已经过滤了完全匹配。 -
^^ 克劳斯是对的。请检查您的
propertyNames收藏的内容。你可能会发现,他们甚至不在里面。解决方案是删除&& property.Name == propertyLabel并稍后检查...或在此处进行不区分大小写的检查。这对你来说可能很有趣:case-insensitive-ordinal-comparisons
标签: c# asp.net asp.net-web-api