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

Iterate FilterDescriptors

5 Answers 192 Views
DataFilter
This is a migrated thread and some comments may be shown as answers.
Richard Harrigan
Top achievements
Rank 1
Richard Harrigan asked on 26 Feb 2011, 12:12 AM
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

5 Answers, 1 is accepted

Sort by
0
Richard Harrigan
Top achievements
Rank 1
answered on 26 Feb 2011, 09:27 PM
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
0
Rossen Hristov
Telerik team
answered on 28 Feb 2011, 10:14 AM
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!
0
Richard Harrigan
Top achievements
Rank 1
answered on 28 Feb 2011, 09:32 PM
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
0
Rossen Hristov
Telerik team
answered on 01 Mar 2011, 09:40 AM
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!
0
Sargis
Top achievements
Rank 1
answered on 02 Dec 2011, 02:11 AM
What if the "value" contains spaces ?
I mean is the white space the best choice as a separator ?
Tags
DataFilter
Asked by
Richard Harrigan
Top achievements
Rank 1
Answers by
Richard Harrigan
Top achievements
Rank 1
Rossen Hristov
Telerik team
Sargis
Top achievements
Rank 1
Share this question
or