如何从编译的LambdaExpression获取计算值? [英] How to get a calculated value from a compiled LambdaExpression?

查看:527
本文介绍了如何从编译的LambdaExpression获取计算值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在上班的一个项目,我们正在使用Telerik RadControls for Silverlight。在2011年第Q1季度,他们添加了一个称为Rad表达式编辑器的新控件。在Rad表达式编辑器中,您可以传入对象并创建公式(或表达式),编辑器窗口将为您预览表达式的结果。我已经和Telerik谈过这个,他们故意没有公开这个的结果,但是提到我可以使用LambdaExpression.Compile()。我对Linq很新,并且一般使用Lambda表达式,但是开始研究这一点。

I'm currently working on a project at work, and we are using the Telerik RadControls for Silverlight. In their Q1 2011 release, they added a new control called the Rad Expression Editor. In the Rad Expression Editor, you can pass in an object and create formulas (or expressions), and the editor window will give you a preview of the result of the expression. I've spoken with Telerik about this and they intentionally did not expose the result of this, but mentioned that I could use LambdaExpression.Compile(). I'm very new to Linq and using Lambda Expressions in general, but started looking into this.

举个例子,我说我有一个名为Finances的对象,该对象有4个可空的十进制字段(值):借记(10),借方(100),信用(20)和信用(200)。在公式中,我想做一些像Debit - Credit + DebitYTD - CreditYTD。

As an example, lets say I have an object called Finances, and in that object, there are 4 nullable decimal fields (values): Debit (10), DebitYTD (100), Credit (20) and CreditYTD (200). In the formula, I want to do something like: Debit - Credit + DebitYTD - CreditYTD.

Telerik Rad表达式编辑器将给出我生成的表达式:
ExpressionEditor.Expression = {Param_0 => ((Param_0.Debit - Param_0.Credit + Param_0.DebitYTD - Param_0.CreditYTD}

The Telerik Rad Expression Editor will give me the expression that is generated: ExpressionEditor.Expression = {Param_0 => ((Param_0.Debit - Param_0.Credit + Param_0.DebitYTD - Param_0.CreditYTD}

此表达式的结果应为-110。我需要能够获得表达式中计算的值,但是无法弄清楚如何获取此数字,任何人都可以解释如何实现这一点。谢谢。

The result of this expression should be -110. I need to be able to get the value that is calculate in the expression, but have not been able to figure out how to get this number. Can anyone please explain how this can be accomplished? Thanks.

推荐答案

你没有真正告诉我们有关它暴露的API,但是它可以像你可以使用的那样:

You haven't really told us much about the API which it exposes, but it sounds like you could use:

var compiled = ExpressionEditor.Expression.Compile();
var result = compiled(input);

其中输入是类型为的适当变量财务

编辑:好的,因为表达式没有很好地展示:

Okay, as the expression isn't exposed nicely:

var typeSafe = (Expression<Func<Finance, decimal?>>) ExpressionEditor.Expression;
var compiled = typeSafe.Compile();
var result = compiled(input);

这篇关于如何从编译的LambdaExpression获取计算值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆