提取功能来自Expression< [英] Extracting Func<> from Expression<>
问题描述
我想从以下表达式中提取Func<>:
I wanna extract the Func<> from the following Expression :
Expression<Func<IQueryable<Entity>, IOrderedQueryable<Entity>>> order = q => q.OrderByDescending(c=>c.FullName);
Func<IQueryable<Entity>, IOrderedQueryable<Entity>> orderFunc = ?
我该怎么办?
以及如何将 Func< IQueryable< Entity>,IOrderedQueryable< Entity>
转换为 Expression< Func< IQueryable< Entity>,IOrderedQueryable< Entity>></code>吗?
And how can we convert Func<IQueryable<Entity>, IOrderedQueryable<Entity>>
to Expression<Func<IQueryable<Entity>, IOrderedQueryable<Entity>>>
?
推荐答案
您可以使用 Compile
方法将任何 Expression< TDelegate>
转换为 TDelegate
.
You can use the Compile
method to turn any Expresstion<TDelegate>
into a TDelegate
.
无法将委托转换为 Expression< TDelegate>
.当表达式被编译为委托时,有关组成表达式的详细信息会丢失.
There is no way to convert a delegate into an Expression<TDelegate>
. The detailed information about what makes up the expression was lost when it was compiled into a delegate.
从理论上讲,您可以通过执行类似的操作来创建一个表达式,该表达式的主体除了调用给定的委托人之外什么都不做:
You could, in theory create an expression who's body does nothing but invoke the given delegate, by doing something like this:
Func<int> function = () => 42;
Expression<Func<int>> expression = () => function();
,但是这样的表达并没有真正的用处.该表达式内部确实没有任何有意义的信息.因此,尽管在技术上可行,但从未真正实用.
but such an expression isn't really useful. There really isn't any meaningful information inside of that expression. So while it's technically possible, it's never really practical.
这篇关于提取功能来自Expression<的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!