Condividi tramite


Retrieving more data from Azure Mobile Services using paging and LoadAllAsync extension

Azure Mobile Services allow you to take 50 records at a time by default, or 1000 records at maximum. What if you have more records and want to retrieve the entire table? I just created this beautiful extension method to help you get any amount of data you want, given any page size you want. This simple call to LoadAllAsync will asynchronously load ALL data from a WAMS table in pages of 1000 (or whatever number you specify) records (may also be good for bandwidth reasons):

 var updatedReports = await azureTable.Where(r => r.complete == true).LoadAllAsync();

And this is an extension method, which elegantly does exactly what it says: loads all data. Enjoy:

         public async static Task<List<T>> LoadAllAsync<T>(this MobileServiceTableQuery<T> table, int bufferSize = 1000)
        {
            var query = table.IncludeTotalCount();
            var results = await query.ToEnumerableAsync();
            long count = ((ITotalCountProvider)results).TotalCount;
            if (results != null && count > 0)
            {
                var updates = new List<T>();
                while (updates.Count < count)
                {

                    var next = await query.Skip(updates.Count).Take(bufferSize).ToListAsync();
                    updates.AddRange(next);
                }
                return updates;
            }

            return null;
        }

This seems like a pretty easy and elegant solution to most queries.