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

标签 c# .net performance linq directory

我有以下查询

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

关于c# - LINQ 链接中的 Any() 运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22275193/

相关文章:

C# 反射 - 获取未知字段的值

c# - 使用简单注入(inject)器 3.1.2 注册 IUserStore

c# - 使用 Moq `It.Is` 验证带有私有(private)字段的构造函数参数/对象

c# - .net 核心 2.1 :Angular SPA Template error : The Angular CLI process did not start listening for requests within the timeout period of 50 sec

sql - 如果根据 EXPLAIN QUERY PLAN 它应该是相反的,为什么一个 SQL 查询比另一个慢?

java - 计算插入后 HashMap 值的平均值

c++ - WinAPI/C++ 中的光照事件

c# - 如何序列化到现有文件?

C# .NET 正确的事件订阅和取消订阅

.net - 处置动态创建的控件