1

In my silverlight project I have many views that all need the same method, "Tester()". Obviously I want this method to be resusable but I am just not getting there.

What I have is this:

    void Tester(IList<MyEntity> list)
    {
        var sortedlist=list.OrderBy(me=>me.Sortkey).ToList();
    }

This works fine, but only for MyEntity.

What I really want is essentially this:

 void Tester<T>(IList<T> list, string mySortField)
    {
        var sortedlist=list.OrderBy("mySortField").ToList();
    }

Doesn't compile.

Any ideas how to do this, please ? Thx in advance, Frank

bluewater
  • 53
  • 1
  • 6

1 Answers1

0

You could do this:

void Tester<T>(IList<T> list, Func<T, object> mySort) {
    var sortedlist=list.OrderBy(mySort).ToList();
}
...
Tester<MyEntity>(list, t => t.mySortField);

Although it's a little more verbose than passing a string, at least you get compile-time checking of sort fields.

Blorgbeard
  • 93,378
  • 43
  • 217
  • 263