我试图创建一个表达式树,它类似于像进行子查询:算术运算
SELECT (SELECT Sum(Foo) FROM Bar1) - (SELECT Sum(Foo) FROM Bar2))
我想重用2个表达式树过于复杂重复。
我现在所拥有的是2(简体)表达式树:
Expression<Func<Bar, int>> SumBar1 =
(bar) => (from b in bar.Something
where b.Type = 1
select b).Sum();
Expression<Func<Bar, int>> SumBar2 =
(bar) => (from b in bar.Something
where b.Type = 2
select b).Sum();
我已经尝试过使用Expression.Subtract
:
Expression foo = Expression.Subtract(SumBar1, SumBar2);
这失败,出现错误:
The binary operator Subtract is not defined for the types 'System.Func
2[Bar,System.Int32]' and 'System.Func
2[Bar,System.Int32]'.
我也尝试使用Expression.Invoke
调用树木:
Expression.Subtract( Expression.Invoke(SumBar1,Expression.Parameter(typeof(Bar)), Expression.Invoke(SumBar2,Expression.Constant(typeof(Bar))));
但后来我得到:
The LINQ expression node type 'Invoke' is not supported in LINQ to Entities.
有什么办法来组合这两个表达式树成一个新的树,减去他们,并沿参数传递?
非常感谢! – 2010-10-22 15:27:38