Hi,
So, I'm a bit out of my comfort zone when dealing with Func<>, Generics and lambda expressions but I think I get the general idea (sort of) but still a bit confused.
I've implemented the SortableObservableCollection class (taken from online somewhere - thanks to whoever it was I got it from!) and it is used like this:
_lookuplistViewModel.Sort(x => x.BrandName, ListSortDirection.Ascending);
where x is the object type implemented by the sortable collection. In this instance, BrandName is a property of the type of object implemented, but I want to use the above code in a generic class and pass in the property on which to sort. The Sort method looks like this:
public void Sort<TKey>(Func<T, TKey> keySelector, ListSortDirection direction)
{
switch (direction)
{
case ListSortDirection.Ascending:
{
ApplySort(Items.OrderBy(keySelector));
break;
}
case System.ComponentModel.ListSortDirection.Descending:
{
ApplySort(Items.OrderByDescending(keySelector));
break;
}
}
}
The generic class on which the Sort method is called is defined like this:
public class ExtendedLookupManagerViewModel<VMod, Mod> : LookupManagerViewModel
where VMod : ExtendedLookupViewModel
where Mod : ExtendedLookupModelBase
and I'd like to create an instance of it like this:
_medProd = new ExtendedLookupManagerViewModel<MedicinalProductViewModel, MedicinalProduct>(string property);
where property
is the property on which to sort. Ideally this should be type safe, but a string will suffice.
Can anyone help steer me in the right direction please?