looping examples on Expression editor and queries

2 posts, 1 answers
  1. Mallikarjun
    Mallikarjun avatar
    12 posts
    Member since:
    Jan 2011

    Posted 25 Aug 2011 Link to this post


    I couldn't find much documentation on RadExpressionEditor. I have some quires below.
    • I am unable to find a sample or build a simple expression using the Logical "If" statement. Please provide me some sample.
    • I have a requirement where the user/Administartor of my web application will build the expression on my business object using the RadExpressionEditor. However, the result is not evaluated. This will be happening at runtime by another windows service.  I want this expression to be saved as a string in my xml file. Later a windows service will read the xml file, evaluate the expression to get the result. Note: My windows service is running on Framework 4.0. Any sample code to achieve this is greatly appreciated. 
    • I see most of the samples of RadExpressionEditor are with RadGridView. I assume RadExpressionEditor control can be used independently with out RadGridView. 

    I am new to silverlight & .NET Expressiontree concept. Please bare if my questions are basic. 

  2. Answer
    Maya avatar
    4062 posts

    Posted 31 Aug 2011 Link to this post

    Hi Mallikarjun,

    The IF expression may be used as IF(expression, ifTrue, ifFalse):
    IF(FirstName = "John", "This is John", "This is not John")

    Considering the second issue, you may create a LambdaExpression, which you can compile. Depending on whether the user used any of the fields of the object in the Item property, RadExpressionEditor can also create a LambdaExpression, so you may need to check if the produced expression is not a LambdaExpression first.
    For example:
    var expression = radExpressionEditor1.Expression;
    // check if the expression is not a LambdaExpression already
    var lambda = expression as LambdaExpression;
    if (lambda == null)
        // create a LambdaExpression from RadExpressionEditor's expression
        lambda = Expression.Lambda(expression);
    // compile the LambdaExpression to a Delegate
    var del = lambda.Compile();

    You can then call the DynamicInvoke method of the Delegate. If the user entered a boolean expression, you can cast the Delegate to Func<Entity, bool>, where Entity is the type of the object in the Item property, which should also match the type of the Entity you would like to test against:
    // make sure that lambda.Type == typeof(bool) && lambda.Parameters.Count == 1 && lambda.Parameters[0].Type = typeof(Entity) before attempting this
    var func = del as Func<Entity, bool>;
    var entity = GetSomeEntity();
    if (func.Invoke(entity))
        // do something

    Right now the expression returned by RadExpressionEditor does not emit the same string it was parsed from when calling its ToString() method, because we can't control the string representation of the built-in LINQ Expression types in .NET. In order to persist the expression, I recommend to save the value of the ExpressionText property, which is the string that the user entered.
    Whenever you need to programatically parse an expression string, you can use the TryParse extension method on RadExpressionEditor, passing the string you saved previously. This method is declared in the Telerik.Windows.Controls namespace, so make sure you have a using for it in your file.
    As for the last question, you are not obliged to use the RadExpressionEditor only with RadGridView. 

    Kind regards,
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Back to Top