查询表达式解析(1)
IEnumerable<string> query = from s in names
where s.Length == 5
orderby s
select s.ToUpper();
在语义上等同于如下“方法风格(基于方法)的查询”:
IEnumerable<string> query = names
.Where(s => s.Length == 5)
.OrderBy(s => s)
.Select(s => s.ToUpper());
查询表达式解析(2)
注意其中的参数为lambda 表达式,类似于如下委托:
Func<string, bool> filter = delegate (string s) {
return s.Length == 5;};
Func<string, string> extract = delegate (string s) {
return s; };
Func<string, string> project = delegate (string s) {
return s.ToUpper(); };
IEnumerable<string> query = names.Where(filter)
.OrderBy(extract)
.Select(project);