Matt Cashatt
Matt Cashatt

Reputation: 24208

Is it possible to make LINQ's OrderBy operation maintain the original order of an IQueryable?

When attempting to page a search result set based on an IQueryable I get an error stating that I must call OrderBy before using Skip.

In most cases, this is fine as I can just order the results by Id. However, I have an edge case where in I am calling a Function Import via my entity class. This function import uses a FREETEXT search in SQL and returns a ranked result set.

I need to maintain--and be able to page through--that result set, but if I call OrderBy against any of the columns in my result set, I will no longer have a set of ranked search results.

So, as silly as it sounds, is there a way to fool LINQ into thinking that I have called an OrderBy operation that just maintains the original order?

Upvotes: 0

Views: 286

Answers (1)

Ahmad Ibrahim
Ahmad Ibrahim

Reputation: 1925

You can use this:

var page = searchResultSet.OrderBy(x => 0).Skip(40).Take(20);

That will maintain the original order of your result set, because all records will have the same constant value of 0, hence sorting them won't change their order after comparing their equal values.

Check this answer

Upvotes: 1

Related Questions