简体   繁体   English

在另一个表达式中评估C#表达式

[英]Evaluate C# expression inside another expression

I want to use an expression in another one: 我想在另一个中使用表达式:

Expression<Func<double, double>> f = x => x * x * 27 + blah ... expression with x;

Expression<Func<double, double>> g = y => 3 + 8 * f.Compile()(y) * y * blah... expression with y and f(y);

This will not work when sent to LINQ to SQL because f.Compile() is unknown to SQL. 这在发送到LINQ to SQL时不起作用,因为SQL不知道f.Compile()。

How do you evaluate the expression f on the variable y without compiling it, but still using normal syntax to define g ? 如何在不编译它的情况下评估变量y上的表达式f ,但是仍然使用常规语法来定义g

I don't want to have to define all of g with some unreadable Expression.Add / Expression.Multiply etc. statements. 我不想用一些不可读的Expression.Add / Expression.Multiply等语句定义所有g

Thanks. 谢谢。

Take a look at Calling functions in LINQ queries and LINQ Extensions project. 看看LINQ查询LINQ Extensions项目中的调用函数 CLinq part is irrelevant to your question, but it also includes LinqExt library, which is just what are you looking for. CLinq部分与您的问题无关,但它也包括LinqExt库,这正是您正在寻找的。 The same approach is also used by LinqKit which also provides other useful extensions for Linq. LinqKit也使用相同的方法,它也为Linq提供了其他有用的扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM