【发布时间】:2011-12-02 01:25:19
【问题描述】:
我喜欢 Linq 语法和它的强大功能,但有时我就是不明白为什么事情是这样工作的。
就像现在一样。我有以下代码:
Regex regex = new Regex( ... );
int result1 = stringList.Count(regex.IsMatch);
IEnumerable<string> result2 = stringList.Where (x => regex.IsMatch (x));
正如您在第一个查询中看到的,我可以使用较短的方法组“regex.IsMatch”,但在第二个查询中我必须编写“x => regex.IsMatch (x)”。
As Count 和 Where 都采用相同类型的参数
Func<string, bool>
执行此操作时,我不明白为什么会出现编译器错误:
IEnumerable<string> result2 = stringList.Where (regex.IsMatch);
【问题讨论】:
标签: .net linq linq-to-objects