I am trying to build a generic Filter Control that displays unique values in the data grid, and lets the user filter the unique values for a particular column in the grid.
I liked the answers proposed for this question, however all of those require you to know the property before hand. I would like to make the code OCP compliant and have just one method that takes the property name string and applies distinct function on it (maybe using reflection). What is the best solution to this problem, considering my columns (thereby column names to filter, like Name, Age, etc. are dynamic).
Specifically, I am trying to avoid this switch case statement:
switch (listColumn.DisplayMemberPath)
{
case "Name" :
listColumn.Items = GridItems.GroupBy(item => item.Name).Select(item => item.First());
break;
case "Age" :
listColumn.Items = GridItems.GroupBy(item=> item.Age).Select(item => item.First());
break;
// and so on...
}
and have a generic method like this:
public IEnumerable GetDistinctValues(IEnumerable gridItems, string propertyName)
{
// logic to return the distinct values for the given property name...
}
FYI - My collection in the ViewModel is of type ICollectionView (guessing that there is something similar already defined in the CollectionView that does the kind of filtering I am looking for).