函数编程和LINQ [英] Functional programming and LINQ

查看:42
本文介绍了函数编程和LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在阅读CodeProject的优秀文章之一

我遇到了以下代码行的麻烦:



< pre lang =C#> string [] names = new string [] { test1 test2};

Predicate< string> longWords = 委托字符串字){ return word.Length > 4 ; };
int numberOfBooksWithLongNames = names.Count(longWords);





编译器错误CS1928和CS1503在最后一行。



这是我的石器时代C#编译器(VS2010)还是出了什么问题?



提前谢谢

解决方案

Predicate< string> 更改为 Func< string,bool>

Linq 方法计数定义为;

public static int Count< TSource>(此IEnumerable< TSource>源,Func< TSource,bool>谓词);



希望这会有所帮助,

Fredrik


At the moment I am reading one of the excellent articles of CodeProject
and I have troubles with code lines like:

string[] names = new string[] {"test1", "test2"};

Predicate<string> longWords = delegate(string word) { return word.Length > 4; };
int numberOfBooksWithLongNames = names.Count(longWords);



Compiler errors CS1928 and CS1503 in the last line.

Is it my stone-age C# compiler (VS2010) or what is going wrong?

Thanks in advance

解决方案

Change Predicate<string> to Func<string, bool>.
The Linq method Count is defined as;
public static int Count<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);

Hope this helps,
Fredrik


这篇关于函数编程和LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆