Why orderBy does not work for this linq expression?
c#, entity-framework, linq
Solution
I think the `OrderBy` method returns a collection rather than ordering against the given collection. Try changing it to the following:
var orderedList = approvedSearchResults.OrderBy(s => s.PostCreatedTime);
Problem
I am trying to order by a datetime field, a list with objects like: ``` // 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); ``` The issues is that nothing gets sorted. Initial order is: ``` 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 ``` After sorting from above (it goes on DESC), it remains the same while this works perfectly in 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 ``` I am making any mistake in my linq?