在MatchCollection上使用LINQ扩展方法语法
问题描述:
我有以下代码:
MatchCollection matches = myRegEx.Matches(content);
bool result = (from Match m in matches
where m.Groups["name"].Value.Length > 128
select m).Any();
是否可以使用LINQ扩展方法语法来做到这一点?
Is there a way to do this using the LINQ extension method syntax?
像这样的东西:
bool result = matches.Any(x => ... );
答
using System.Linq;
matches.Cast<Match>().Any(x => x.Groups["name"].Value.Length > 128)
您只需要将其从 IEnumerable 转换为 IEnumerable< Match> (IEnumerable< T>)来访问IEnumerable< T>上提供的LINQ扩展。
You just need to convert it from an IEnumerable to an IEnumerable<Match> (IEnumerable<T>) to get access to the LINQ extension provided on IEnumerable<T>.