首先,我在發布此問題之前進行了搜索,但答案非常籠統,以至于我找不到與我正在嘗試做的類似的事情。如果之前已經回答過并且我沒有找到它,我會提前道歉。

我有一個場景,我已經從 SQL 服務器收集了大量行 (>10K) 并放入字串陣列 (List) 中。這些行由檔案名組成。因為我已經將它們放在一個串列中,所以我不想再次查詢 SQL 服務器,而是希望使用我已經在記憶體中擁有的內容。
這是我試圖正確的代碼:
'InfoLink1、InfoLink2、InfoLink3 的有效檔案型別
Dim lstValidImageFormats As New List(Of String)({".JPG", ".JPEG", ".JPE", ".BMP", ".PNG", ".TIF", ".TIFF", ".GIF"})
Dim lstValidSTLFormats As New List(Of String)({".STL"})
Dim lstValidSTEPFormats As New List(Of String)({".STP", ".STEP"})
'////////////////
'// Components //
'////////////////
'We don't check Parts.InfoLink because all formats are allowed in this field
'Parts.InfoLink1 - File in Infolink1 columns MUST BE images
For i = 0 To arrStrComponentsInfolink1Values.Count - 1 'We have 10K rows with filenames in this list
For Each FileExtension As String In lstValidImageFormats
If arrStrComponentsInfolink1Values.Item(i).EndsWith(FileExtension) = False Then
End If
Next
Next
我正在嘗試決議陣列中的每個專案(檔案名)arrStrComponentsInfolink1Values 并檢查檔案名是否不以串列中的擴展名之一結尾lstValidImageFormats。如果沒有,那么我會將檔案名發送到另一個串列(陣列)。
我的困難在于如何遍歷 in 中的每個專案arrStrComponentsInfolink1Values,然后檢查每個檔案名是否以 in 中宣告的擴展名之一結尾,如果lstValidImageFormats專案不以其中一個擴展名結尾,則執行我想要對專案執行的操作,然后繼續決議arrStrComponentsInfolink1Values.
我真的不知道這樣做的最佳方式/性能效率是什么。
我上面的代碼是空的,因為從演算法上講,我不知道最好的方法來做我想做的事,而無需再次使用類似的東西查詢 SQL 服務器 AND filename NOT LIKE '%.JPG' AND filename NOT LIKE '%.JPEG' AND filename NOT LIKE '%.JPE' AND filename NOT LIKE '%.BMP'...
因為我已經將記憶體中的資料放在一個串列中,所以如果我可以使用我已經擁有的資料,性能會好得多。
我可以閱讀任何建議或材料來學習如何做我正在尋找的東西?
謝謝!
uj5u.com熱心網友回復:
以下是我將如何解決這個問題:
Dim invalidFormatFiles = _
From x In arrStrComponentsInfolink1Values _
Let fi = New FileInfo(x) _
Where Not lstValidImageFormats.Contains(fi.Extension.ToUpperInvariant()) _
Select x
For Each invalidFormatFile In invalidFormatFiles
' Do your processing
Next
uj5u.com熱心網友回復:
我最終這樣做了,它奏效了:
Dim lstValidImageFormats As New List(Of String)({".JPG", ".JPEG", ".JPE", ".BMP", ".PNG", ".TIF", ".TIFF", ".GIF"})
Dim lstValidSTLFormats As New List(Of String)({".STL"})
Dim lstValidSTEPFormats As New List(Of String)({".STP", ".STEP"})
'////////////////
'// Components //
'////////////////
'We don't check Parts.InfoLink because all formats are allowed in this field
'Parts.InfoLink1 - File in Infolink1 columns MUST BE images
Dim intExtCounter As Integer = 0
For i = 0 To arrIntComponentsInfolink1UNRs.Count - 1 'We have 10K rows with filenames in this list
intExtCounter = 0
For j = 0 To lstValidImageFormats.Count - 1
If arrStrComponentsInfolink1Values.Item(i).EndsWith(lstValidImageFormats.Item(j)) = True Then
intExtCounter = 1
End If
Next
If intExtCounter = 0 Then 'At least one file extension was found
arrIntComponentsInfolink1UNRsReportSectionInvalidExtensions.Add(i) 'File extension is not in the list of allowed extensions
End If
Next
但是@41686d6564 的答案是最好的解決方案:
Dim newList = arrStrComponentsInfolink1Values.Where(Function(x) Not lstValidImageFormats.Contains(IO.Path.GetExtension(x))).ToList()
謝謝!
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/416489.html
標籤:
下一篇:vb.net選擇陳述句失敗
