Func键<>>表达℃之间的差异;和Func键<>

Func键<>>表达℃之间的差异;和Func键<>

问题描述:

作为一个例子,为什么大多数LINQ运营商接受表达式来; Func键&LT; TSource&GT;&GT; 及其等价物 Func键&LT; TSource&GT;

As an example, why do most LINQ operators accept Expression<Func<TSource>> and its equivalent Func<TSource>?

有什么好处/之所以使用,而不是直线上升lambda语法的一般表达类?

What's the benefit/reason for using the generic Expression class instead of straight up lambda syntax?

使用表达式来; T&GT; 你明确地创建一个 表达式树的 - 这意味着你可以处理,使得代码了查询,好像它是数据

Using Expression<T> you are explicitly creating an expression tree - this means that you can deal with the code that makes up the query as if it were data.

这样做的原因是,LINQ提供(如LINQ到SQL例如)检查查询本身来确定的最佳方式C#的表达式转换成T-SQL查询。由于表达式树可以让你看看代码为数据提供者能够做到这一点。

The reason for this is that LINQ providers (like LINQ to SQL for example) inspect the query itself to determine the best way to translate the C# expressions into a T-SQL query. Since an expression tree lets you look at the code as data the provider is able to do this.