Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using LINQ extension method syntax on a MatchCollection

Tags:

c#

linq

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 => ... );
like image 377
Thomas Avatar asked Sep 01 '11 17:09

Thomas


People also ask

What is LINQ extension method?

Linq provides standard query operators like filtering, sorting, grouping, aggregation, and concatenations, and it has many operators to achive many types of functionalities, which are called extension methods, in LINQ.

Does LINQ use extension methods?

Short answer: Yes, you can use a custom extension method inside a LINQ query - but you cannot use an extension method that the underlying data provider does not know how to execute. LINQ stands for Language-Integrated-Query, and is a language feature in C#. You can use any . NET method in a LINQ query.

When should you use LINQ in your program?

LINQ in C# is used to work with data access from sources such as objects, data sets, SQL Server, and XML. LINQ stands for Language Integrated Query. LINQ is a data querying API with SQL like query syntaxes. LINQ provides functions to query cached data from all kinds of data sources.


4 Answers

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

like image 72
msarchet Avatar answered Oct 17 '22 14:10

msarchet


When you specify an explicit range variable type, the compiler inserts a call to Cast<T>. So this:

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

is exactly equivalent to:

bool result = matches.Cast<Match>()
                     .Where(m => m.Groups["name"].Value.Length > 128)
                     .Any();

which can also be written as:

bool result = matches.Cast<Match>()
                     .Any(m => m.Groups["name"].Value.Length > 128);

In this case the Cast call is required because MatchCollection only implements ICollection and IEnumerable, not IEnumerable<T>. Almost all the LINQ to Objects extension methods are targeted at IEnumerable<T>, with the notable exceptions of Cast and OfType, both of which are used to convert a "weakly" typed collection (such as MatchCollection) into a generic IEnumerable<T> - which then allows for further LINQ operations.

like image 36
Jon Skeet Avatar answered Oct 17 '22 15:10

Jon Skeet


Try this:

var matches = myRegEx.Matches(content).Cast<Match>();

For reference, please see Enumerable.Cast:

Converts the elements of an IEnumerable to the specified type.

Basically it's one way of turning an IEnumerable into an IEnumerable<T>.

like image 8
Andrew Hare Avatar answered Oct 17 '22 13:10

Andrew Hare


I think it would be something like this:

bool result = matches.Cast<Match>().Any(m => m.Groups["name"].Value.Length > 128);
like image 3
pstrjds Avatar answered Oct 17 '22 14:10

pstrjds