我正在嘗試列出第一個串列中的所有元素,其中包含等于第二個串列中所有元素的子字串
第一個清單:
C:\Folder\Files_01026666.pdf
C:\Folder\Files_01027777.pdf
C:\Folder\Files_01028888.pdf
C:\Folder\Files_01029999.pdf
第二個清單:
01027777
01028888
串列結果應該是:
C:\Folder\Files_01027777.pdf
C:\Folder\Files_01028888.pdf
我得到的越接近,.Intersect()但兩個字串元素都應該相等
List<string> resultList = firstList.Select(i => i.ToString()).Intersect(secondList).ToList();
List<string> resultList = firstList.Where(x => x.Contains(secondList.Select(i=>i).ToString()));
List<string> resultList = firstList.Where(x => x == secondList.Select(i=>i).ToString());
我知道我可以用另一種方式做到這一點,但我想用 LINQ 做到這一點。我查看了其他查詢,但我可以找到與 Linq 的密切比較。您可以指出我的任何想法或任何地方都會有很大幫助。
uj5u.com熱心網友回復:
我們可以使用EndsWith()with Path.GetFileNameWithoutExtension(),因為 secondList 中的字串不是完整的檔案名。
var result = firstList
.Where(path => secondList.Any(fileName => Path.GetFileNameWithoutExtension(path).EndsWith(fileName)));
在線試用
uj5u.com熱心網友回復:
var q = list1.Where(t=>Regex.IsMatch(t,String.Join("|",list2.ToArray()))));
似乎適用于字串串列。在 LINQ 中使用 Regex 可能是個問題。例如,這在 Linq2SQL 中不起作用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/446628.html
