C# 如何编写字段搜索,而不考虑其大小写?
网站上有一个过滤功能,用于查找公共财产的价值: ? propertyName=SomeName&propertyValue=SomeValue 现在我想知道,例如,如果输入了这个属性的错误名称(SomENAme1),web应用程序还会调用SomeName属性过滤什么?也就是说,不管大小写是什么都不重要,输入的单词至少与filter属性完全匹配C# 如何编写字段搜索,而不考虑其大小写?,c#,asp.net,asp.net-web-api,C#,Asp.net,Asp.net Web Api,网站上有一个过滤功能,用于查找公共财产的价值: ? propertyName=SomeName&propertyValue=SomeValue 现在我想知道,例如,如果输入了这个属性的错误名称(SomENAme1),web应用程序还会调用SomeName属性过滤什么?也就是说,不管大小写是什么都不重要,输入的单词至少与filter属性完全匹配 public static IQueryable<T> ApplyFiltering<T>(this IQueryable<
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);
}
但是它不起作用,即使只是小写…在你的LINQ的
中,你已经过滤了一个精确匹配。^^Klaus是对的。请检查您的propertyNames
收藏的内容。你可能会发现,它们甚至都不在里面。解决方案是删除&&property.Name==propertyLabel
,稍后再检查。。。或者在这里进行现场检查。这可能会让你感兴趣:
var propertyNames = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public)
.Where(property => property.PropertyType == typeof(string)
&& propertyLabel.ToLower().Contains(property.Name.ToLower()))
.Select(property => property.Name);
var propertyNames = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public)
.Where(property => property.PropertyType == typeof(string)
&& propertyLabel.ToLower().Contains(property.Name.ToLower()))
.Select(property => property.Name);