I have a dynamic sort mechanism which I am using to do a dyanmic LINQ OrderBy. This works fine on ordinary fields.
string sortField = "MyField"
var orderByParam = Expression.Parameter(typeof(MyType), "MyType");
var sortExpression = Expression.Lambda<Func<MyType, object>>(Expression.Property(orderByParam, sortField), orderByParam);
However when I try to use a Nullable field (which happens to be a DateTime) I get the following error:
Expression of type 'System.Nullable`1[System.DateTime]' cannot be used for return type 'System.Object'
How can I get round this?