Supreet
Supreet

Reputation: 841

Paging & Sorting with Azure Mobile Services C# Backend

What's wrong with the following code?

 int skipCount = pageIndex * 20;
 var query = questionsTable.Where(x => x.AskedAtCityId == city.Id).OrderByDescending(x => x.AskedAt).Take(20).Skip(skipCount);
 query.IncludeTotalCount(); 

 await query.ToListAsync().ContinueWith(t =>
 {
     if (t.Status == TaskStatus.RanToCompletion)
     {
         tcs.SetResult(t.Result);
     }
 });

I intend to get some data that is sorted, filtered & paged. The result set I get is filtered fine, but it's not sorted by createdAt date.

Firing the query a second time even jumbles up the data & also the data is not continuous. I am getting all random results.

What I need is a list of most recently asked questions, sorted by date, descending & in pages of 20 records.

What's missing?

Thanks

Upvotes: 1

Views: 91

Answers (1)

Supreet
Supreet

Reputation: 841

The query is just fine. I did make a small change though. Skip() first & then Take() Also I was getting the jumbled up records because the index I was sending as a parameter had a miscalculated value in it. Fixing that bit of code & now everything is just fine. Thanks

Upvotes: 1

Related Questions