I have a Table class to which I pass a generic list of objects called data. Since the objects stored in the list can be of various type I implemented the following Table class
public class Table<T>
{
private List<T> _data;
public Table(List<T> data)
{
_data = data;
//other constructor stuff
}
}
The idea is that the Table class will "build" a table listing as colums all the public properties of the T - type object stored in the data.
I managed to achieve this by doing _data[0].GetType().GetProperties()
to retrieve the properties and to build the columns header. I then implemented a click event on the column titles so that by clicking on it the data would get sorted by this property. Here is where the problem is. I cannot get Linq to sort by the given property since at compile time the type of the object is not known.
I tried with
_data = _data.OrderBy(e => e.GetType().GetProperty("Surname")).ToList<T>();
Where Surname
is a property that I know my test object (stored in the _data) has.
I don't get any errors but it seems the instruction to order doesn't do anything. What is wrong? Is there a less convoluted way of doing this? (BTW I'm not using WPF so I guess there is not point in using DataTable).
I found similar questions answered already but in all of them the the T-type of the List was known at compile time.
Thanks for your help.