Barney Chambers
Barney Chambers

Reputation: 2783

Azure MobileService Client - ToListAsync not retrieving all data

I am using the MobileServiceClient class provided by Azure to fetch data from my Azure SQL database. It appears that I am only getting the first 60 or so rows from my database when I use the ToListAsync() function. Is there a way around this?

List<riskregister_hazard_template> categories;

categories = await riskTable.Where(r => r.level_1 == _level1)
.Where(r => r.level_2 == _level2).ToListAsync();

Upvotes: 0

Views: 235

Answers (1)

Alberto Morillo
Alberto Morillo

Reputation: 15618

Please read the following excerpt:

By default, a read operation in a table controller will return up to 50 items. If we have more in our table storage, then the client will need to request more, by casting the result of the ToListAsync or ToEnumerableAsync methods to the IQueryResultEnumerable interface. The code below shows how to go through all elements in the table.

 public async Task<double> CalculateAverageAge()
{
    var client = new MobileServiceClient(AppUrl, AppKey);
    var table = client.GetTable<Person>();
    var sum = 0.0;
    var count = 0;
    var items = await table.Take(10).ToEnumerableAsync();
    while (items != null && items.Count() != 0)
    {
        count += items.Count();
        sum += Enumerable.Sum(items, i => i.Age);

        var queryResult = items as IQueryResultEnumerable<Person>;
        if (queryResult != null && queryResult.NextLink != null)
        {
            items = await table.ReadAsync<Person>(queryResult.NextLink);
        }
        else
        {
            items = null;
        }
    }

    return sum / count;
}

Source: https://azure.microsoft.com/en-us/blog/better-support-for-paging-with-table-storage-in-azure-mobile-services-net-backend/

Hope this helps.

Regards,

Alberto Morillo

SQLCoffee.com

Upvotes: 1

Related Questions