Here’s a wild one. I’m writing a universal entity editor using JQuery and NHibernate LINQ. So far everything works great. But now I’m trying to add sorting support. Things have gotten tricky. I want to tell it to sort on a property, but in the class, I will not know what kind of object it is until runtime.
Here’s the code:
data =
(from entity in _repository.FindAll()
orderby entity.GetType().GetProperty(formData["sidx"]).GetValue(entity, null)
select entity).ToList();
Here’s the problem, I get a null pointer exception when I do this. Any clues on how I can do what I want to do? I suspect this issue is going to come up again when I do searching.
Use Dynamic LINQ (http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx, section Downloading the LINQ Dynamic Query Library) and specify the field to sort by as a string: