我有一個IEnumerable <DirectoryInfo>我想使用正則表達式數組來篩選可能的匹配。我一直試圖加入我的目錄和正則表達式字符串使用linq,但似乎無法做到正確。這就是我想要做的...使用正則表達式列表查找匹配的目錄
string[] regexStrings = ... // some regex match code here.
// get all of the directories below some root that match my initial criteria.
var directories = from d in root.GetDirectories("*", SearchOption.AllDirectories)
where (d.Attributes & (FileAttributes.System | FileAttributes.Hidden)) == 0
&& (d.GetDirectories().Where(dd => (dd.Attributes & (FileAttributes.System | FileAttributes.Hidden)) == 0).Count() > 0// has directories
|| d.GetFiles().Where(ff => (ff.Attributes & (FileAttributes.Hidden | FileAttributes.System)) == 0).Count() > 0) // has files)
select d;
// filter the list of all directories based on the strings in the regex array
var filteredDirs = from d in directories
join s in regexStrings on Regex.IsMatch(d.FullName, s) // compiler doesn't like this line
select d;
...有關如何讓這個工作的任何建議?
啊,Any()上的通話很好。我一直在忘記那些! – 2009-07-08 18:42:03