Reputation: 5
I have created a helper method to retrieve rows in descending order with continuationtoken using Cosmos DB C# Library. However it returns no rows when I try order by descending. The query works without orderby or orderbydescending. Please let me know what I need to correct to retrieve rows in correct order.
public static async Task<(IEnumerable<T> resultRows, string responseContinuation)> GetIAlltemsAsyncOrderByCount(Expression<Func<T,bool>> predicate, Expression<Func<T,object>> orderby,bool descending, int MaxItemCountPerQuery,string continuationToken="")
{
var uri = UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId);
var feedOptions = new FeedOptions
{
MaxItemCount = MaxItemCountPerQuery
};
string responseContinuation = "";
if (!string.IsNullOrEmpty(continuationToken))
{
feedOptions.RequestContinuation = continuationToken;
responseContinuation = continuationToken;
}
var resultRows = new List<T>();
using (var query = descending ? client.CreateDocumentQuery<T>(uri, feedOptions).Where(predicate).OrderByDescending(orderby).AsDocumentQuery() : client.CreateDocumentQuery<T>(uri, feedOptions).Where(predicate).OrderBy(orderby
).AsDocumentQuery())
{
if (query.HasMoreResults)
{
var result = await query.ExecuteNextAsync<T>();
responseContinuation = result.ResponseContinuation;
resultRows.AddRange(result);
/* process result */
}
}
return (resultRows, responseContinuation);
}
Upvotes: 0
Views: 1242
Reputation: 18465
According to your description, I checked this issue on my side and found I could encounter the similar issue. Then I used Fiddler to capture the network traces as follows:
As indexing-policies states as follows:
Index kind: Hash (equality queries), Range (equality, range or ORDER BY queries), or Spatial (spatial queries) .
Range supports efficient equality queries, range queries (using >, <, >=, <=, !=), and ORDER BY queries. ORDER By queries by default also require maximum index precision (-1). The data type can be String or Number.
I log into portal to choose my Cosmos DB account and modify the Indexing Policy under Scale & Settings of my collection as follows:
Based on the above changes, I could order by the string data type.
Upvotes: 1