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

Data Filter and Inheritance

4 Answers 106 Views
DataFilter
This is a migrated thread and some comments may be shown as answers.
Matt
Top achievements
Rank 1
Matt asked on 11 Mar 2011, 10:12 PM
Hello,

I'm working with a RadDaraFilter and a GridView.

The data I show is a list of instances of multiple types that derive from a base type. The derived types have their own set of properties.

Knowing that the data filter wasn't going to go through my derived types' properties, I populated the filter item property definition manually.
Unfortunately, it seems like the data filter only does the filtering on the properties of the base type, or the one the generic collection is of.

Example:
class BaseType
{
string Name { get; set; }
}

class DerivedType1 : BaseType
{
string SubData { get; set; }
}

class DerivedType2 : BaseType
{
string AnotherSubData { get; set; }
}

var list = new List<BaseType>();
// add DerivedType1 and 2 instances to list.
dataFilter.Source = list;

My filter contains the item property definitions for Name, SubData and AnotherSubData.
- Filtering on Name is the only thing that works (guessing because it's in the base type).
- Filtering on SubData and AnotherSubData always results in an empty list in the grid view.

Is there any workarounds for this?

Thanks!
Matt

4 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 14 Mar 2011, 10:10 AM
Hello Matt,

That is impossible.

RadDataFilter will only work with the generic type of the collection, which can be BaseType, but can't be the two sub-types at the same time.

You can think about this filtering task in terms of a LINQ query.

If you have a generic collection of BaseType, what kind of LINQ Where would you write in order to filter the collection on both SubData and AnotherSubData:

var collection = new ObservableCollection<BaseType>().AsQueryable();

var result = collection.Where(item => item.SubData == "something); --> this will not compile
var result = collection.Where(item => item.AnotherSubData == "something); --> this will not compile

I hope this makes sense.

Kind 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
Matt
Top achievements
Rank 1
answered on 14 Mar 2011, 08:55 PM
Hello Ross,

I agree that the link queries would make no sense written this way. However, it's probably not the way it's done underneath is it?

The properties and types for the filtering are passed as a string and a type, so you might be doing some reflection calls to get the properties of the base type. Isn't it possible to extend that to the possible types of the ItemsSource? Or at least allow the users to provide a list of possible types?
Even if we don't supply the types, the code could always query the property on the current type being filtered and if it's not there, handle it.


If not, do you suggest I flatten my hierarchy of classes to include everything in the base type? It doesn't seem very practical... although it might be my only option here, or perhaps I could override the connection between the filter and the grid view and do the filtering myself.

--
Matt

0
Rossen Hristov
Telerik team
answered on 15 Mar 2011, 08:17 AM
Hi Matt,

Our data engine is using LINQ, be it LINQ to Objects, LINQ to SQL, LINQ to Entities, etc.

In fact, it does not really know what is the exact LINQ provider. It simply generates LINQ expressions and let's the LINQ provider do the job.

For example, if you have LINQ to SQL, the whole filtering will be translated to an SQL Where clause on the SQL Server. You can't do that with reflection.

We are not using reflection. This would be a complete and total overkill in terms of performance. Imagine iterating over 1 000 000 items and checking whether each one of them has a certain property and whether this property has a certain value. That's just not plausible.

So, I guess you will have to flatten the hierarchy out. Your other option would be to use RadDataFilter without specifying a Source, i.e. Unbound Mode. Then manually add any kinds of ItemProperty definitions. When the user adds filters, this information will be added to the FilterDescriptors collection of the Filter. You can iterate over them and gather all the filtering criteria. From then on, you can use any kind of reflection that you want in order to filter the original data. But this might have performance implications, so it would be up to you.

I hope this helps.

All the best,
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
Matt
Top achievements
Rank 1
answered on 15 Mar 2011, 07:15 PM
Thanks for the reply. I already started with the flattening, it's working, thanks.

--
Matt
Tags
DataFilter
Asked by
Matt
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Matt
Top achievements
Rank 1
Share this question
or