Take this simple SPQuery for example:

SPQuery query = new SPQuery();
query.Query = "<Where><IsNotNull><FieldRef Name='Modified' /></IsNotNull></Where>";
query.RowLimit = 10;

Notice that "OrderBy" is not defined in the query.

In this case, how are the results of the SPQuery sorted before the top 10 records are selected to be returned?

Will the above query return 10 random items since no "OrderBy" is defined?


EDIT: The reason for this question was a mistake I made where I performed sorting on the query results instead of using "OrderBy" in the query. After setting RowLimit, I could not get the most recently modified items from a list.

The lesson here is to use "OrderBy" instead of sorting the results returned by the query when RowLimit is set.

有帮助吗?

解决方案

If you don't specify the Order by in "SPQuery" as mention above the data return will Order by ID/Id of item of the list in Ascending order.

For Quick result you can check with rest api on browser as below:

siteURL/_api/web/lists/getByTitle('ListName')/items?$Top=5
许可以下: CC-BY-SA归因
scroll top