2013-05-13 24 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