系统.IO.FileInfo不区分大小写的搜索

3
以下是一些我修改过并最初从这里复制的代码: 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

我已经搜索了几个小时,但找不到答案。如果有人能帮忙,我将非常感激。

1个回答

3
这应该可以解决问题:
where file.Name.IndexOf(searchString, StringComparison.CurrentCultureIgnoreCase) >= 0

(当没有匹配的子字符串时,IndexOf 返回 -1)

没错,就是这样。谢谢! - Jordan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接