这是原始的Linq:
var developersUsingCSharp =
from d in developers
where d.Language == "C#"
select d.Name;
这可以写成:
Func<Developer, bool> filteringPredicate = d => d.Language == "C#";
Func<Developer, string> selectionPredicate = d => d.Name;
IEnumerable<string> developersUsingCSharp =
developers
.Where(filteringPredicate)
.Select(selectionPredicate);
上面我们已经明确定义了 Func <T,T> 委托。所以在
Func<Developer, bool> filteringPredicate = d => d.Language == "C#";
已知 d 为 Developer 类型,返回类型为 bool。同样,对于
Func<Developer, string> selectionPredicate = d => d.Name;
已知 d 为 Developer 类型,返回类型为字符串。但是,在相同查询的以下有效形式中,这些类型是如何推断的:
IEnumerable<string> developersUsingCSharp =
developers
.Where(d => d.Language == "C#")
.Select(d => d.Name);