Skip to main content
 首页 » 编程设计

c#之LINQ 链接中的 Any() 运算符

2024年02月13日12wayfarer

我有以下查询

Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories).Any() 

如果存在任何一个 .txt 文件,则上述表达式返回 true。我怀疑上面的递归枚举是否在第一次出现 .txt 时停止,或者 Directory.GetFiles() 给出所有 .txt 的列表,然后 Any() 函数将应用于 GetAllFiles() 发送的 IEnumerable。

我的意图是查找给定路径是否至少有任何一个 txt 文件

谢谢

请您参考如下方法:

my doubts is does the above recursive enumeration stops on first occurance of .txt

不,Directory.GetFiles 返回string[] 所以它读取所有文件。使用 Directory.EnumerateFiles 代替

Directory.EnumerateFiles(path, "*.txt", SearchOption.AllDirectories).Any();