This is a migrated thread and some comments may be shown as answers.

Exposing Result Preview from Expression Editor

4 Answers 169 Views
ExpressionEditor
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Eric asked on 29 Apr 2011, 02:16 PM
In another thread, I asked about an ETA for the documentation for the expression editor.  After looking through the properties on the expression editor using a break point and some sample/temp data, I see that the expression editor "does" do at least half of what I need it to do, which is calculate user defined formulas from an object that is passed in.  Basically, what I'm doing here is, we have a report where the user can choose which columns are shown.  These are all decimal values, with the exception of 3 columns.  Well, the user can also "create" a column using a formula containing any of the available decimal columns.

In order to get the fields list displayed the way I wanted, I had to create another object whose only fields/properties are the the ones that contain the available columns from the report.  The new object is then passed to the expression editor as the Item.  Here's an example:

NewObject (contains the following fields):
Debit
Credit
DebitYTD
CreditYTD
DebitAnnual
CreditAnnual

So far at this point, the expression editor does what I want.  I can create the formula using the expression editor (i.e. Debit + Credit).  In the expression editor's result preview field, the values are calculated correctly.  However, since there is no documentation, I haven't been able to figure out how to get the value of the result preview from the expression editor.  It's in the view model for the expression editor, but it's not public.  Is there anything that is exposed that will give me the result of that calculation, as long as the expression is valid?  Thanks.

Eric G.

4 Answers, 1 is accepted

Sort by
0
Yavor Georgiev
Telerik team
answered on 29 Apr 2011, 06:38 PM
Hello Eric,

 The result preview of the ExpressionEditor is meant to be non-public. You should compile the LambdaExpression and execute it on the item yourself. RadExpressionEditor is not meant to execute an expression - simply to generate it. We are considering providing this functionality in the future, but we have not decided whether we will extend RadExpressionEditor or develop a new component.

 If you want to control which fields from the item's type are shown in the ExpressionEditor, you should decorate the properties of your type with the Display data annotation attribute.

Greetings,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Eric
Top achievements
Rank 1
answered on 02 May 2011, 01:02 PM
Thank you for the information Yavor.  I'm trying to find some information on using the LamdaExpression to compile what's generated from the Expression Editor.  Do you have a C# example of this?  Also, do you have an ETA on when the documentation for the Expression Editor will be released?  Thanks.
0
Eric
Top achievements
Rank 1
answered on 03 May 2011, 01:57 PM
This is just a quick update for anyone else having this problem as well.  I was able to get the answer from Jon Skeet from StackOverflow.  This will return the calculated value that shows up in the result preview.  The code to get the value is (Note, "Finances" is the type of object that you used to load the Fields in the expression editor, and "this.vm.FormulaColumnsList" is the actual object that is bound to it):

//Used to get the calculated value from the expression
if (ExpressionEditor.IsExpressionValid)
{
    //Courtesy of Jon Skeet from StackOverflow
    var typeSafe = (Expression<Func<Finances, decimal?>>)ExpressionEditor.Expression;
    var compiled = typeSafe.Compile();
    var result = compiled(this.vm.FormulaColumnsList);
}
0
Denis Vuyka
Top achievements
Rank 1
answered on 07 Jun 2011, 04:48 PM
My update for anyone else having this problem as well. You may want to read the detailed explanation here.
ExpressionEditor already contains an expression ready to be compiled. To avoid creating strongly typed expressions (in many complex scenarios it is impossible to know the type of result value) you can use "dynamic" objects (DLR that is part of Silverlight + Microsoft.CSharp.dll reference) to compile and execute expression.

Here's a quick usage sample:

dynamic dynamicExpression = expressionEditor.Expression;
dynamic compiledExpression = dynamicExpression.Compile();
object executionResult = compiledExpression(myInstance);

Hope that helps,
Denis
Tags
ExpressionEditor
Asked by
Eric
Top achievements
Rank 1
Answers by
Yavor Georgiev
Telerik team
Eric
Top achievements
Rank 1
Denis Vuyka
Top achievements
Rank 1
Share this question
or