System.NotSupportedException创建反转列表时 [英] System.NotSupportedException when creating a reversed list
问题描述
我正在尝试以降序排列返回按插入日期排序的状态更新列表。此列表显示在部分视图中,因此无法使用控制器对列表进行排序。解决方案是创建一个Service类,其中包含以下代码:
I'm trying to return a list of status updates, that's ordered by inserted date, in a descending order. This list is displayed in a partial view, so I'm unable to use the controller to sort the list. The solution was to create a Service class that includes this code:
var StatusResult = (from status in db.UserStatuses
orderby status.DateInserted
select status).Reverse().ToList();
return StatusResult;
当我运行它时,我收到这个错误:
When I run it, I get this error:
在EntityFramework.SqlServer.dll中发生类型为System.NotSupportedException的异常,但未在用户代码中处理
An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code
其他信息:LINQ to Entities不识别方法'System.Linq.IQueryable 1 [Project.Models.UserStatus] Reverse [UserStatus](System.Linq.IQueryable
1 [Project .Models.UserStatus])'方法,而且这个方法不能被翻译成一个存储表达式。
Additional information: LINQ to Entities does not recognize the method 'System.Linq.IQueryable1[Project.Models.UserStatus] Reverse[UserStatus](System.Linq.IQueryable
1[Project.Models.UserStatus])' method, and this method cannot be translated into a store expression.
任何人都可以帮助我了解什么关于和如何解决这个问题?
Can anyone help me understand what's going on and how to fix this?
推荐答案
EF不知道如何告诉SQL Server反转查询的结果,这就是为什么你的查询执行失败的原因。虽然可能,但更好的方法是简单地按照降序排序查询结果:
EF doesn't know how to tell SQL Server to reverse the results of a query, which is why your query is failing when it executes. Although it is possible, the better way to do it is to simply ask for the query results sorted in descending order:
var StatusResult = (from status in db.UserStatuses
orderby status.DateInserted descending
select status).ToList();
return StatusResult;
这篇关于System.NotSupportedException创建反转列表时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!