There is a table Customers with a nullable cstCredit column. The following works fine and can be iterated.
var query = _oContext.Customers.OrderBy(cst => cst.cstCredit);
However, the following will fail when iterating.
public IQueryable<Customer> AllSorted(Expression<Func<Customer, object>> orderby)
{
return _oContext.Customers.OrderBy(orderby);
}
void test()
{
var query = AllSorted(cst => cst.cstCredit);
foreach (Customer oCustomer in query)
{
}
}
The message (translated from German) is
“The type System.Nullable can not be converted to System.Object. LINQ
to Entities only supports converting primitive or enumeration types”.
What have I done wrong?
Try writing the
AllSortedmethod like this:It didn’t work because nullable types like
int?are value types are do not derive from object ( and are instances of theSystem.Nullablestruct) and henceExpression<Func<Customer, object>> orderbywould not work with them.