Exposing Result Preview from Expression Editor

5 posts, 0 answers
  1. Eric
    Eric avatar
    6 posts
    Member since:
    Apr 2011

    Posted 29 Apr 2011 Link to this post

    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):

    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.
  2. Yavor Georgiev
    Yavor Georgiev avatar
    982 posts

    Posted 29 Apr 2011 Link to this post

    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.

    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
  3. DevCraft banner
  4. Eric
    Eric avatar
    6 posts
    Member since:
    Apr 2011

    Posted 02 May 2011 Link to this post

    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.
  5. Eric
    Eric avatar
    6 posts
    Member since:
    Apr 2011

    Posted 03 May 2011 Link to this post

    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);
  6. Denis Vuyka
    Denis Vuyka avatar
    39 posts
    Member since:
    Oct 2009

    Posted 07 Jun 2011 Link to this post

    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,
Back to Top
DevCraft banner