2013-05-13 21 views
1

首先,我知道這是this question的副本,但我無法獲得列出的解決方案爲我工作。我明白,MatchCollection沒有實現IEnumerable Parallel.ForEach用法,因此需要OfType()...任何想法我做錯了什麼?這裏是我的設置:Parallel.ForEach with MatchCollection

MatchCollection startMatches = Regex.Matches(tempRTB.Text, startPattern); 

System.Threading.Tasks.Parallel.ForEach(startMatches.OfType<Match>, m => 
{ 
    // do stuff with m 
}); 

而這裏的編譯錯誤,我得到:

Error 11 The type arguments for method 'System.Threading.Tasks.Parallel.ForEach<TSource>(System.Collections.Generic.IEnumerable<TSource>, System.Action<TSource>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. 
+2

注意'MatchCollection ''元素總是'匹配',所以你可以使用'.Cast ()'而不是'OfType'。 – 2013-05-13 18:10:24

回答

4

所有你缺少的是()(OfType是一個靜態擴展方法)

System.Threading.Tasks.Parallel.ForEach(startMatches.OfType<Match>(), m => 
     { 
      // do stuff with m 
     }); 
+0

......該死。 :P謝謝哈哈 – Hershizer33 2013-05-13 18:09:35