Iterate FilterDescriptors

6 posts, 0 answers
  1. Richard Harrigan
    Richard Harrigan avatar
    235 posts
    Member since:
    Nov 2009

    Posted 25 Feb 2011 Link to this post

    Hi,

    Do you have any sample code that recursively  iterates thru the FilterDescriptors taking into account all the levels of And/Or.   Also do you have any example of generating the SQL WHERE clause from the FilterDescriptors.

    Thanks
    RIch
  2. Richard Harrigan
    Richard Harrigan avatar
    235 posts
    Member since:
    Nov 2009

    Posted 26 Feb 2011 Link to this post

    Some additional information.  I cannot use Linq 2 Sql since I am not using a data model and therfore cannot get a table using DataContext.GetTable<not available>.  I don't think it would be that difficult to generate the sql if could iterate the FilterDescriptors (FilterDescriptor and CompositeFilterDescriptor) correctly sa that I can keep my And's and Or's straight..

    Thanks
    RIch
  3. DevCraft banner
  4. Rossen Hristov
    Admin
    Rossen Hristov avatar
    2478 posts

    Posted 28 Feb 2011 Link to this post

    Hi Richard Harrigan,

    We don't have a special example for iterating over the FilterDescriptors. The data structure of the filter descriptors is a tree, so basically the task is to traverse a tree.

    If you call the ToString method of RadDataFilter.FilterDescriptors property you will receive a pseudeo-code where expression. All of this is achieved by overriding the ToString methods of the CompositeFilterDescriptor and the FilterDescriptor classes.

    So you can do something similar. I will paste the two ToString overrides for your reference:

    FilterDescriptor.ToString:

    public override string ToString()
    {
        string value;
        if (this.Value == FilterDescriptor.UnsetValue)
        {
            value = "<Unset>";
        }
        else
        {
            value = this.Value != null ? this.Value.ToString() : "null";
        }
         
        return string.Format(CultureInfo.InvariantCulture
            , "{0} {1} {2} {3}"
            , this.Member ?? "null"
            , this.Operator
            , value
            , this.IsCaseSensitive ? "MC" : "");
    }

    CompositeFilterDescriptor.ToString:

    public override string ToString()
    {
        if (this.FilterDescriptors.Count == 0)
        {
            return "Empty";
        }
     
        StringBuilder result = new StringBuilder();
        result.Append("( ");
        for (int i = 0; i < this.FilterDescriptors.Count; i++)
        {
            if (i > 0)
            {
                result.Append(" " + this.LogicalOperator.ToString().ToUpper() + " ");
            }
     
            result.Append("("+this.FilterDescriptors[i].ToString()+")");
        }
        result.Append(" )");
     
        return result.ToString();
    }

    So to achieve your task you have to do something similar. In other words -- when you encounter a "simple" FilterDescriptor (a tree leaf) you will return its Member-Operator-Value expression. When you encounter a CompositeFilterDescriptor -- you will return all of its children combined with its LogicalOperator in between.

    This code library can also give you some useful ideas. It is for WPF, but since RadControls share a common code-base between WPF and Silverlight -- the code will be applicable to Silverlight as well.

    I hope this helps.

    Regards,
    Ross
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  5. Richard Harrigan
    Richard Harrigan avatar
    235 posts
    Member since:
    Nov 2009

    Posted 28 Feb 2011 Link to this post

    Hi

    I guess I need to create my version of the RadDataFilter class.  Do I also need to create my own versions of FilterDescriptor and CompositeFilterDescriptor classes?    Could you please provide a code snippet that shows a skelton of the class I need to create. 

    Thanks
    Rich
  6. Rossen Hristov
    Admin
    Rossen Hristov avatar
    2478 posts

    Posted 01 Mar 2011 Link to this post

    Hi Richard Harrigan,

    The ToString method was just an example. Don't follow it blindly. This example just demonstrated how recursion is used to traverse a tree.

    Your task is to recursively traverse a tree. I am sure you know how to use recursion and traverse a tree.

    Regards,
    Ross
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  7. Sargis
    Sargis avatar
    1 posts
    Member since:
    Dec 2011

    Posted 01 Dec 2011 Link to this post

    What if the "value" contains spaces ?
    I mean is the white space the best choice as a separator ?
Back to Top
DevCraft banner