我需要一些使用LINQ来根据文件大小来归档文件列表的帮助。我有一些代码,但是它使用的是file.length而不是FileInfo(file).length。我不知道如何在表达式中实现对象“ FileInfo”。救命?
{
IEnumerable<string> result = "*.ini,*.log,*.txt"
.SelectMany(x => Directory.GetFiles("c:\logs", x, SearchOption.TopDirectoryOnly))
;
result = result
.Where(x => (x.Length) > "500000")
;
}
Run Code Online (Sandbox Code Playgroud)
您应该能够执行这样的操作。
使用DirectoryInfo,GetFiles将返回FileInfo的集合,而不是字符串。
new DirectoryInfo(@"c:\logs")
.GetFiles("*.ini,*.log,*.txt", SearchOption.TopDirectoryOnly)
.Where(f => f.Length > 500000);
Run Code Online (Sandbox Code Playgroud)
当然,如果需要,您总是会内联创建FileInfo。
如果您只想返回文件名。
IEnumerable<string> results = new DirectoryInfo(@"c:\logs")
.GetFiles("*.ini,*.log,*.txt", SearchOption.TopDirectoryOnly)
.Where(f => f.Length > 500000)
.Select(f => f.FullName);
Run Code Online (Sandbox Code Playgroud)