Extracting Func<> from Expression<>

匆匆过客 提交于 2021-02-08 13:14:22

问题


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 = ?

How can I do it?

Edit :

And how can we convert Func<IQueryable<Entity>, IOrderedQueryable<Entity>> to Expression<Func<IQueryable<Entity>, IOrderedQueryable<Entity>>> ?


回答1:


You can use the Compile method to turn any Expresstion<TDelegate> into a 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.



来源:https://stackoverflow.com/questions/20368638/extracting-func-from-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!