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;
        }

One thought on “Retrieving more data from Azure Mobile Services using paging and LoadAllAsync extension

  1. Pingback: Windows Azure Action – Community Newsletter – 13th February 2013 | MSDN Blogs

Leave a Reply

Your email address will not be published. Required fields are marked *


1 + = three

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>