Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在.net core 3x中,Regex Match不能将组输入到可枚举扩展 请考虑此代码: var regEx=新的regEx(“样本”); var match=regEx.match(“一些示例文本”); var a=match.Groups.Skip(1).First().Name;_C#_Linq_.net Core - Fatal编程技术网

C# 在.net core 3x中,Regex Match不能将组输入到可枚举扩展 请考虑此代码: var regEx=新的regEx(“样本”); var match=regEx.match(“一些示例文本”); var a=match.Groups.Skip(1).First().Name;

C# 在.net core 3x中,Regex Match不能将组输入到可枚举扩展 请考虑此代码: var regEx=新的regEx(“样本”); var match=regEx.match(“一些示例文本”); var a=match.Groups.Skip(1).First().Name;,c#,linq,.net-core,C#,Linq,.net Core,这将在2.2下编译和工作,但在3.1中编译错误: error CS1061: 'GroupCollection' does not contain a definition for 'Skip' and no accessible extension method 'Skip' accepting a first argument of type 'GroupCollection' could be found (are you missing a using directive or an a

这将在2.2下编译和工作,但在3.1中编译错误:

error CS1061: 'GroupCollection' does not contain a definition for 'Skip' and no accessible extension method 'Skip' accepting a first argument of type 'GroupCollection' could be found (are you missing a using directive or an assembly reference?)
有趣的是,将其更改为:

var regEx=新的regEx(“样本”);
var match=regEx.match(“一些示例文本”);
IEnumerable groups=match.groups;
var a=groups.Skip(1).First().Name;
将修复错误


为什么会出现此错误?是什么破坏性的更改导致了此错误?

这是因为“GroupCollection”在Linq中没有“skip”的扩展方法,因此您需要将其强制转换为“IEnumerable”,以使用Linq的“skip”扩展方法


您编写的第二个方法是将其强制转换为IEnumerable或调用ToList()使其成为“IEnumerable”。

这是由于以下更改:

根据讨论,如果不被认为是突破性的变更,那么是否在任何变更日志中都没有提及:

如果它在预览中被捕获,我们可能会还原它;但是考虑到我们已经发布了一个LTS版本,它可能已经永远存在了

有点讽刺的是,我们在今天的API回顾中讨论了IEnumerable和IReadonlyDictionary的重载歧义问题,但我们显然忽略了这一点,因为它在扩展方法调用上存在歧义,因为现在有两个不同的IEnumerable

建议使用cast来获取类型化的
可枚举的

 int matches = m.Groups.Cast<Group>().Count(t => t.Success);
int matches=m.Groups.Cast().Count(t=>t.Success);

这是否回答了您的问题:?是否尝试使用组的值,如:match.Groups.Values.Skip(1).First().Name;