在使用lambda表达式时,是否存在特别有帮助的情况,或者它的主要用途是为了写更少的代码?
引入lambda表达式的原因有两点。
(1) 它们使得查询推导式的语法转换成为可能。当你说:
from customer in customers
where customer.City == "London"
select customer
那就这样变成了。customers.Where(customer=>customer.City == "London")
(2) 它们可以转化为表达式树,从而使得LINQ-to-SQL、LINQ-to-Entities等成为可能。也就是说,它们可以同时代表执行语义的能力和检查其结构的能力。