C# 在MatchCollection上使用LINQ扩展方法语法

C# 在MatchCollection上使用LINQ扩展方法语法,c#,linq,C#,Linq,我有以下代码: MatchCollection matches = myRegEx.Matches(content); bool result = (from Match m in matches where m.Groups["name"].Value.Length > 128 select m).Any(); 有没有办法使用LINQ扩展方法语法来实现这一点 大概是这样的: bool result = m

我有以下代码:

MatchCollection matches = myRegEx.Matches(content);

bool result = (from Match m in matches
               where m.Groups["name"].Value.Length > 128
               select m).Any();
有没有办法使用LINQ扩展方法语法来实现这一点

大概是这样的:

bool result = matches.Any(x => ... );
bool result = matches.Cast<Match>().Any(m => m.Groups["name"].Value.Length > 128);
List<Match> matchList = matches.Cast<Match>().Where(m => m.Groups["name"].Value.Length > 128).ToList();
试试这个:

var matches = myRegEx.Matches(content).Cast<Match>();
var matches=myRegEx.matches(content.Cast();
有关参考资料,请参阅:

IEnumerable
的元素转换为指定类型

基本上,这是使用System.Linq将
IEnumerable
转换为
IEnumerable

的一种方法;
匹配.Cast().Any(x=>x.Groups[“name”].Value.Length>128)
您只需将其从
IEnumerable
转换为
IEnumerable
(IEnumerable),即可访问IEnumerable上提供的LINQ扩展。

编辑:

 public static IEnumerable<T> AsEnumerable<T>(this IEnumerable enumerable)
 {
      foreach(object item in enumerable)
          yield return (T)item;
 }
公共静态IEnumerable AsEnumerable(此IEnumerable可枚举)
{
foreach(可枚举中的对象项)
收益回报(T)项;
}
然后您应该能够调用此扩展方法将其转换为IEnumerable:

 matches.AsEnumerable<Match>().Any(x => x.Groups["name"].Value.Length > 128);
matches.AsEnumerable().Any(x=>x.Groups[“name”].Value.Length>128);

我想应该是这样的:

bool result = matches.Any(x => ... );
bool result = matches.Cast<Match>().Any(m => m.Groups["name"].Value.Length > 128);
List<Match> matchList = matches.Cast<Match>().Where(m => m.Groups["name"].Value.Length > 128).ToList();
bool result=matches.Cast().Any(m=>m.Groups[“name”].Value.Length>128);
当指定显式范围变量类型时,编译器将插入对
Cast
的调用。因此:

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

bool result = matches.Cast<Match>()
                     .Where(m => m.Groups["name"].Value.Length > 128)
                     .Any();
bool result=matches.Cast()
.其中(m=>m.Groups[“name”].Value.Length>128)
.Any();
也可以写为:

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

在这种情况下,需要调用
Cast
,因为只实现
ICollection
IEnumerable
,而不是
IEnumerable
。几乎所有LINQ到对象的扩展方法都是针对
IEnumerable
,除了类型
Cast
之外,这两种方法都用于转换“弱”类型的集合(例如
MatchCollection
)进入一个通用的
IEnumerable
——然后允许进一步的LINQ操作。

您可以尝试以下方法:

bool result = matches.Any(x => ... );
bool result = matches.Cast<Match>().Any(m => m.Groups["name"].Value.Length > 128);
List<Match> matchList = matches.Cast<Match>().Where(m => m.Groups["name"].Value.Length > 128).ToList();
List matchList=matches.Cast().Where(m=>m.Groups[“name”].Value.Length>128.ToList();

这比我的好,我不记得有人用谓词。没有。关键是
MatchCollection
只实现
IEnumerable
。它不是强类型。@Jason,只是可以通过IEnumberable转换为IEnumberable。Cast@msarchet:是的,我知道,这就是为什么我对你的答案投了赞成票。在编辑之前,这个答案甚至不会被编译。关键是
MatchCollection
只实现
IEnumerable
。它不是强类型。这很有效,只要确保您使用的是System.Linq
,否则它会给出一个语法错误谢谢您,对于任何困惑的人,
Cast
从C#8.0开始就不需要了,但是如果没有提供,代码将不会在早期语言版本中编译。我发现如果我将项目升级到NetStandard2.1 match.Any()成功。在MSDN上: