tags:

views:

162

answers:

3

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?

+1  A: 

Well, your Sort method is only generic in TKey - where does T come from? I suspect it should either be Func<VMod, TKey> or Func<Mod, TKey> but I'm unsure which from what you've shown.

What would BrandName be a property of - MedicinalProductViewModel or MedicinalProduct? Assuming it's MedicinalProduct, your method should be declared as:

public void Sort<TKey>(Func<Mod, TKey> keySelector, ListSortDirection direction)

At that point I suspect it will work...

Jon Skeet
T is the type of SortableObservableCollection (public class SortableObservableCollection<T> : ObservableCollection<T>). BrandName is a property of both since VMod is a viewmodel wrapping Mod (Model). The problem is that BrandName is a property of a specific type (ie. MedicinalProductViewModel) so I have to use this object type to run the sort method. I want to able to use any type derived from ExtendedLookupViewModel and specify the property to sort on instead of BrandName but I don't know how to replace x.BrandName with my passed parameter.
pilsdumps
@pilsdumps: But you're not calling Sort on the `SortableObservableCollection<T>` - you're calling it on `ExtendedLookupManagerViewModel<VMod, Mod>`. Have you tried the change I suggested? (Possibly using `VMod` instead of `Mod`?)
Jon Skeet
+2  A: 

You should not be accepting a string property argument, but rather a Func<T, IComparable> where T is probably either VMod or Mod, depending what you are trying to sort.

recursive
Ok, I'd considered that but I'm unsure how I'd pass that in. I think TKey is the key (property) on T to sort on, so how would I go about doing it? Somthing like _medProd = new ExtendedLookupManagerViewModel<MedicinalProductViewModel, MedicinalProduct>(Func<VMod, WHAT GOES HERE??); . This will pass the object type, but how do I pass the property?
pilsdumps
+2  A: 

Just make your constructor sig match the sig for the sort method, and cache the params for using in the collection when Sort() is called. So not "string property" but rather whatever the sig is for the sort method.

The passed parameter then would be a func that could be type specific and directing you to the element, the instantiation would be

_medProd = new ExtendedLookupManagerViewModel<MedicinalProductViewModel, MedicinalProduct>(x => x.BrandName, ListSortDirection.Ascending);
Jimmy Hoffa
Thanks folks, problem solved, and in hindsight I am a muppet. I simple added Func<VMod, Object> sortby in the constructor of ExtendedLookupManagerViewModel, and called it as described by Jimmy above. The call to sort now is _lookuplistViewModel.Sort(_sortby, ListSortDirection.Ascending);Thanks again.
pilsdumps