C# 可空列的动态OrderBy失败
有一个表C# 可空列的动态OrderBy失败,c#,linq,dynamic,nullable,C#,Linq,Dynamic,Nullable,有一个表Customers,其cstredit列可为空。以下操作很好,可以进行迭代 var query = _oContext.Customers.OrderBy(cst => cst.cstCredit); 但是,在迭代时,以下操作将失败 public IQueryable<Customer> AllSorted(Expression<Func<Customer, object>> orderby) { return _oContext.Cus
Customers
,其cstredit
列可为空。以下操作很好,可以进行迭代
var query = _oContext.Customers.OrderBy(cst => cst.cstCredit);
但是,在迭代时,以下操作将失败
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)
{
}
}
public-IQueryable-AllSorted(表达式排序依据)
{
return _oContext.Customers.OrderBy(OrderBy);
}
无效测试()
{
var query=AllSorted(cst=>cst.cstCredit);
foreach(查询中的客户用户)
{
}
}
这条消息(翻译自德语)是
“无法将System.Nullable类型转换为System.Object.LINQ
“到实体”仅支持转换基元或枚举类型
我做错了什么?试试这个
public IQueryable<Customer> AllSorted<T>(Expression<Func<Customer, T>> orderby)
{
return _oContext.Customers.OrderBy(orderby);
}
public-IQueryable-AllSorted(表达式排序依据)
{
return _oContext.Customers.OrderBy(OrderBy);
}
试试这个
public IQueryable<Customer> AllSorted<T>(Expression<Func<Customer, T>> orderby)
{
return _oContext.Customers.OrderBy(orderby);
}
public-IQueryable-AllSorted(表达式排序依据)
{
return _oContext.Customers.OrderBy(OrderBy);
}
试着像这样编写AllSorted
方法:
public IQueryable<Customer> AllSorted<TKey>(Expression<Func<Customer, TKey>> orderby)
{
return _oContext.Customers.OrderBy(orderby);
}
public-IQueryable-AllSorted(表达式排序依据)
{
return _oContext.Customers.OrderBy(OrderBy);
}
它不起作用,因为像
int?
这样的可空类型是值类型,它们不是从object派生的(并且是系统的实例。可空的struct),因此表达式orderby
不能使用它们。尝试编写AllSorted
方法,如下所示:
public IQueryable<Customer> AllSorted<TKey>(Expression<Func<Customer, TKey>> orderby)
{
return _oContext.Customers.OrderBy(orderby);
}
public-IQueryable-AllSorted(表达式排序依据)
{
return _oContext.Customers.OrderBy(OrderBy);
}
它不起作用,因为像int?
这样的可空类型是值类型,它们不是从对象派生的(并且是系统的实例。可空的struct),因此表达式orderby
不能使用它们。cstCredit的类型是什么?您可以说对象
(在表达式
)中。可能在之前添加一个.where
。orderby
并检查cst.cstCredit!=null
?cstCredit
是否可以为null?@TravisJ基于它出现的问题。cstCredit
的类型是什么?您可以说对象
(在表达式
)中。可能在之前添加一个.where
。orderby
并检查cst.cstCredit!=null
?是否cstCredit
null?@TravisJ基于它似乎是这样的问题。