将两个表达式组合成管道 [英] Combining two expressions into a pipeline
问题描述
表达式< Func< T,IEnumerable< TNested>> ; collectionSelector;
表达式< Func< IEnumerable< TNested>,TNested>>> elementSelector;
有没有办法结合这些以形成以下内容:(?)
表达式< Func< T,TNested>>选择;
编辑:
性能非常关键如果可能的话,我会很欣赏一个很少的开销的最优解。
非常感谢!
static Expression< Func< A,C>> Foo (
表达式< Func< B,C>> f,
表达式< Func< A,B>>> g)
{
var x = Expression.Parameter(typeof(A));
返回Expression.Lambda< Func< A,C>(
Expression.Invoke(f,Expression.Invoke(g,x)),x);
}
不幸的是,我无法访问计算机(在性能方面是坏的解决方案) 。其实我觉得你可以通过调用Expression来优化代码。
另一种方式似乎是(使用辅助功能):
Func< A,C> Foo< A,B,C>(Func< B,C> f,Func< A,B> g)
{
return(A x)=> F(G(X));
}
然后,您应该通过使用Foo函数调用表达式创建流水线表达式。像伪代码一样:
var expr1 = get expresstion< B,C>
var expr2 = get Expression< A,B>
var foo =获取Foo方法的方法
通用类型A,B,C(通过make通用方法)的specialize方法
返回Expression.Call(foo,expr1,expr2);
Let us say I have the following two expressions:
Expression<Func<T, IEnumerable<TNested>>> collectionSelector;
Expression<Func<IEnumerable<TNested>, TNested>> elementSelector;
Is there a way to "combine" these in order to form the below: (?)
Expression<Func<T, TNested>> selector;
EDIT:
Performance is very critical, so I would appreciate an optimal solution with very little overhead, if possible.
Many Thanks!
static Expression<Func<A, C>> Foo<A, B, C>(
Expression<Func<B, C>> f,
Expression<Func<A, B>> g)
{
var x = Expression.Parameter(typeof(A));
return Expression.Lambda<Func<A, C>>(
Expression.Invoke(f, Expression.Invoke(g, x)), x);
}
Unfortunately, I cannot access to computer ( it is bad solution in performance terms). Actually, I think that you can optimize code via call Expression.
Another way seems like that(usage auxiliary function):
Func<A, C> Foo<A,B,C>(Func<B, C> f, Func<A, B> g)
{
return (A x) => f(g(x));
}
Then you should create pipeline Expression via call Expression with Foo function. Like that pseudo code:
var expr1 = get expresstion<B,C>
var expr2 = get Expression<A, B>
var foo = get method info of Foo method
specialize method with generic types A, B, C (via make generic method)
return Expression.Call(foo, expr1, expr2);
这篇关于将两个表达式组合成管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!