Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.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不适用于此linq表达式?_C#_Linq_Entity Framework - Fatal编程技术网

C# 为什么orderBy不适用于此linq表达式?

C# 为什么orderBy不适用于此linq表达式?,c#,linq,entity-framework,C#,Linq,Entity Framework,我试图按datetime字段排序,这是一个包含以下对象的列表: // Order search results (posts to be displayed by created datetime). if (implicitSelectedVisualiser.PostsSortOrder == PostsSortOrder.CREATED_DATE_ASC) approvedSearchResults.OrderBy(s => s.PostCreatedTim

我试图按datetime字段排序,这是一个包含以下对象的列表:

    // Order search results (posts to be displayed by created datetime).
   if (implicitSelectedVisualiser.PostsSortOrder == PostsSortOrder.CREATED_DATE_ASC)
      approvedSearchResults.OrderBy(s => s.PostCreatedTime);
   else
      approvedSearchResults.OrderByDescending(s => s.PostCreatedTime);
问题是什么都没有分类

初步订单为:

2013-06-28 19:52:08.000
2013-06-28 19:38:30.000
2013-06-28 18:35:37.000
2013-06-29 17:07:22.000
2013-07-01 19:12:44.000
2013-07-01 19:15:29.000
2013-07-01 23:51:11.000
从上面进行排序后(在DESC中继续),它保持不变,而这在SQL中工作得非常好

SELECT [PostCreatedTime]
FROM [SearchResults]
Where SearchQuery_Id = 10 or SearchQuery_Id = 7
Order by PostCreatedTime desc

2013-07-01 23:51:11.000
2013-07-01 19:15:29.000
2013-07-01 19:12:44.000
2013-06-29 17:07:22.000
2013-06-28 19:52:08.000
2013-06-28 19:38:30.000
2013-06-28 18:35:37.000

我在linq中出错了吗?

我认为
OrderBy
方法返回的是一个集合,而不是对给定集合进行排序。尝试将其更改为以下内容:

var orderedList = approvedSearchResults.OrderBy(s => s.PostCreatedTime);

它不是内联排序-您需要将排序后的版本分配给变量

您需要将搜索结果存储在某个位置-目前

approvedSearchResults.OrderByDescending(s => s.PostCreatedTime);
不会存储在任何位置。

请尝试:

approvedSearchResults = approvedSearchResults.OrderBy(s => s.PostCreatedTime);

OrderBy返回一个已排序的集合,但不针对该集合排序。

您确定
隐式选择的Visualizer.PostssorOrder!=PostssorOrder.创建日期ASC