Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 可空列的动态OrderBy失败_C#_Linq_Dynamic_Nullable - Fatal编程技术网

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基于它似乎是这样的问题。