/// <summary> /// Retrieves a collection of entities based on the query and allows you to perform an action against each one. /// </summary> /// <param name="query">A TsQuery object containing a TableQuery.</param> /// <param name="action">The action to perform on each item returned by the query.</param> /// <returns></returns> public async Task <BatchProcessResult> BatchProcessAsync(TsQuery query, Func <List <T>, Task> action) { if (query == null) { throw new ArgumentNullException("query"); } if (action == null) { throw new ArgumentNullException("action"); } TableContinuationToken continuationToken = null; var tableQuery = new TableQuery <T>().Where(query.Query).Take(MAX_BATCH_COUNT); var batches = 0; var entitiesProcessed = 0; do { var tableQueryResult = await _table.ExecuteQuerySegmentedAsync(tableQuery, continuationToken); continuationToken = tableQueryResult.ContinuationToken; await action(tableQueryResult.Results); entitiesProcessed += tableQueryResult.Results.Count; if (tableQueryResult.Results.Count > 0) { batches++; } }while (continuationToken != null); return(new BatchProcessResult(batches, entitiesProcessed)); }
/// <summary> /// Allows you to perform a query on the against the table. /// Warning this could result in a table scan being executed. /// </summary> /// <param name="query">A TsQuery object containing a TableQuery.</param> /// <returns></returns> public async Task <IEnumerable <T> > QueryAsync(TsQuery query) { var returnList = new List <T>(); TableContinuationToken continuationToken = null; var tableQuery = new TableQuery <T>().Where(query.Query).Take(MAX_BATCH_COUNT); do { var tableQueryResult = await _table.ExecuteQuerySegmentedAsync(tableQuery, continuationToken); returnList.AddRange(tableQueryResult.Results); continuationToken = tableQueryResult.ContinuationToken; }while (continuationToken != null); return(returnList); }
/// <summary> /// Deletes all the entites from the table based upon the provided query. /// </summary> /// <param name="query">A TsQuery object containing a TableQuery.</param> /// <returns></returns> public async Task <BatchDeleteResult> BatchDeleteAsync(TsQuery query) { if (query == null) { throw new ArgumentNullException("query"); } TableContinuationToken continuationToken = null; var tableQuery = new TableQuery <T>().Where(query.Query).Take(MAX_BATCH_COUNT); var batches = 0; var deleteBatches = 0; var entitiesDeleted = 0; List <T> entitiesToDelete = new List <T>(); do { var tableQueryResult = await _table.ExecuteQuerySegmentedAsync(tableQuery, continuationToken); continuationToken = tableQueryResult.ContinuationToken; entitiesToDelete.AddRange(tableQueryResult.Results); if (tableQueryResult.Results.Count > 0) { batches++; } }while (continuationToken != null); var results = await DeleteInBatchesAsync(entitiesToDelete); deleteBatches += results.Item1; entitiesDeleted += results.Item2; return(new BatchDeleteResult(batches, deleteBatches, entitiesDeleted)); }