Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何编写字段搜索,而不考虑其大小写?_C#_Asp.net_Asp.net Web Api - Fatal编程技术网

C# 如何编写字段搜索,而不考虑其大小写?

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<

网站上有一个过滤功能,用于查找公共财产的价值: ? propertyName=SomeName&propertyValue=SomeValue

现在我想知道,例如,如果输入了这个属性的错误名称(SomENAme1),web应用程序还会调用SomeName属性过滤什么?也就是说,不管大小写是什么都不重要,输入的单词至少与filter属性完全匹配

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);