我正在嘗試 LINQ 查詢一組檔案,我可以在其中找到帶有特定字串的檔案名。
我正在使用:
var docs = directory.enumerateFiles(searchFolder, "* " strNumber "*", SearchOption.AllDirectories);
這作業正常,但由于其中一個目錄有 1 百萬個檔案,我的一些檔案搜索需要 30 多分鐘。我希望通過 PLINQ 查詢來加快搜索程序。然而,雖然我的語法很好,但我并沒有得到我期望的結果。看起來我的問題可能出在 Where 陳述句中。任何幫助都會有所幫助。
foreach (strNumber in strNumbers)
{
DirectoryInfo searchDirectory = new DirectoryInfo(searchFolder);
IEnumerable<System.IO.FileInfo> allDocs = searchDirectory.EnumerateFiles("*", SearchOPtion.AllDirectories);
IEnumerable<System.IO.FileInfo> docsToProcess = strNumbers
.SelectMany(strNumber => allDocs
.Where(file => file.Name.Contains(strNumber)))
.Distinct();
}
任何幫助將非常感激。
uj5u.com熱心網友回復:
我會改變問題的順序。
- 創建所有檔案的串列(進入記憶體)
- 對記憶體串列執行搜索
然后,您可以在記憶體陣列上使用 Parallel Foreach,并且您的磁盤使用僅限于初始搜索。
var searchDirectory = new DirectoryInfo(searchFolder);
var allDocs = searchDirectory.EnumerateFiles("*", SearchOPtion.AllDirectories).ToArray();
// For extra points, use a Parallel.ForEach here for multi-threaded work
Parallel.Foreach(strNumbers, strNumber =>
{
// Work on allDocs here, it should be in memory
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/454034.html
上一篇:C#泛化排序方式
