The easiest way is the Cast method:
IEnumerable<Match> strongMatches = matches.Cast<Match>();
Note that this is delayed and passes on its data, so you do not have a complete βcollectionβ as such, but it is a great data source for LINQ queries.
Cast automatically called if you specify the type of the range variable in the query expression:
So, to completely convert your request:
public static int MaxSequence(string str) { return (from Match match in Regex.Matches(str, "H+|T+") select match.Value.Length into matchLength orderby matchLength descending select matchLength).First(); }
or
public static int MaxSequence(string str) { MatchCollection matches = Regex.Matches(str, "H+|T+"); return matches.Cast<Match>() .Select(match => match.Value.Length) .OrderByDescending(len => len) .First(); }
In fact, you do not need to call OrderByDescending , and then First here - you just want to get the maximum value that the Max method will receive. Even better, it allows you to specify the projection from the type of the source element to the value you are trying to find, so you can do without Select :
public static int MaxSequence(string str) { MatchCollection matches = Regex.Matches(str, "H+|T+"); return matches.Cast<Match>() .Max(match => match.Value.Length); }
If you have a collection that has some elements of the desired type, but some of which may be missing, you can use OfType . Cast throws an exception when it encounters an element of the "wrong" type; OfType just skips it.
Jon skeet
source share