以下是我修改并最初从此处复制的一些代码:
http://msdn.microsoft.com/en-us/library/bb546159.aspx
我在目录中搜索文件名并且工作正常,但我无法弄清楚如何使其成为不敏感的
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(startFolder);
// This method assumes that the application has discovery permissions
// for all folders under the specified path.
IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
//Create the query
IEnumerable<System.IO.FileInfo> fileQuery =
from file in fileList
where file.Extension == ".pdf"
where file.Name.Contains(searchString, StringComparison.OrdinalIgnoreCase)
orderby file.Name
select file;
此行包含错误:
where file.Name.Contains(searchString, StringComparison.OrdinalIgnoreCase)
错误是:
Error 1 'string' does not contain a definition for 'Contains' and the best extension method overload 'System.Linq.ParallelEnumerable.Contains<TSource>(System.Linq.ParallelQuery<TSource>, TSource, System.Collections.Generic.IEqualityComparer<TSource>)' has some invalid arguments C:\Users\k5opjdjc\Documents\Visual Studio 2013\Projects\Test_File_Search\Test_File_Search\Controllers\SearchController.cs 30 23 Test_File_Search
我已经搜索了几个小时,找不到答案。如果有人可以提供帮助,我将非常感激。
答案 0 :(得分:2)
这应该可以解决问题:
where file.Name.IndexOf(searchString, StringComparison.CurrentCultureIgnoreCase) >= 0
(当没有找到匹配的子字符串时,IndexOf
返回-1)