I heard before that using System.Reflection is ill advised.
I am trying to make a dynamic filter for data gotten form a sqlite database.
Right now, I have a base class that is more or less an overloaded shell for ObservableCollection<T>, that is inherited in a data class that I use to manage my collection of records.
I was thinking that I could use System.Reflection to make filtering the data even more dynamic.
I could basically do something like this...
Other than this being crazy confusing, as far as I know it should work....
public Task<ObservableCollection<WeaponInventoryItem>> Filter(string key, string value)
{
FilteredList = (ObservableCollection<WeaponInventoryItem>)this.Items.Where(x => x.GetType().GetProperties().FirstOrDefault(y => y.Name.Equals(key)).Equals(value));
return Task.FromResult(FilteredList);
}
My alternative would be to make a switch based on the incoming key then just make case entries for each possible filter and make the appropriate `
public Task<ObservableCollection<WeaponInventoryItem>> Filter(string key, string value)
{
switch(key)
{
case "Manufacturer":
FilteredList = (ObservableCollection<WeaponInventoryItem>)this.Items.Where(x => x.Manufacturer.Equals(value)));
break;
}
return Task.FromResult(FilteredList);
}
Any thoughts or suggestions?
Cheers!