C# – Using LINQ extension method syntax on a MatchCollection

clinq

I have the following code:

MatchCollection matches = myRegEx.Matches(content);

bool result = (from Match m in matches
               where m.Groups["name"].Value.Length > 128
               select m).Any();

Is there a way to do this using the LINQ extension method syntax?

Something like this:

bool result = matches.Any(x => ... );

Best Answer

using System.Linq;

matches.Cast<Match>().Any(x => x.Groups["name"].Value.Length > 128)

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>.