/// <summary>
        /// Your documentation here.  (see
        /// http://msdn.microsoft.com/en-us/library/windowsazure/XXXXX.aspx
        /// for more information)
        /// </summary>
        /// <param name="eventQuery">
        /// Required. Your documentation here.
        /// </param>
        /// <returns>
        /// Your documentation here.
        /// </returns>
        public IEnumerable <EventModel> ExecuteEventQuery(
            EventQuery eventQuery)
        {
            // Validate
            if (eventQuery == null)
            {
                throw new ArgumentNullException("eventQuery");
            }

            var shouldTrace  = TracingAdapter.IsEnabled;
            var invocationId = string.Empty;

            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                var tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("eventQuery", eventQuery);
                TracingAdapter.Enter(invocationId, this, "ExecuteEventQuery", tracingParameters);
            }
            EventQueryContinuationToken continuationToken = null;

            do
            {
                var response = ExecuteEventQuerySegmented(eventQuery, continuationToken, null);
                continuationToken = response.ContinuationToken;
                foreach (var eventModel in response.Events)
                {
                    yield return(eventModel);
                }
            } while (continuationToken != null);
        }
 /// <summary>
 /// Your documentation here.  (see
 /// http://msdn.microsoft.com/en-us/library/windowsazure/XXXXX.aspx
 /// for more information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.AzureStack.Management.StorageAdmin.IFarmOperations.
 /// </param>
 /// <param name="eventQuery">
 /// Required. Your documentation here.
 /// </param>
 /// <param name="continuationToken">
 /// Optional. Your documentation here.
 /// </param>
 /// <param name="maxCount">
 /// Optional. Your documentation here.
 /// </param>
 /// <returns>
 /// Your documentation here.
 /// </returns>
 public static Task <EventListResponse> ExecuteEventQuerySegmentedAsync(
     this IFarmOperations operations,
     EventQuery eventQuery,
     EventQueryContinuationToken continuationToken,
     int?maxCount)
 {
     return(operations.ExecuteEventQuerySegmentedAsync(eventQuery, continuationToken, maxCount, CancellationToken.None));
 }
 /// <summary>
 /// Your documentation here.  (see
 /// http://msdn.microsoft.com/en-us/library/windowsazure/XXXXX.aspx
 /// for more information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.AzureStack.Management.StorageAdmin.IFarmOperations.
 /// </param>
 /// <param name="eventQuery">
 /// Required. Your documentation here.
 /// </param>
 /// <param name="continuationToken">
 /// Optional. Your documentation here.
 /// </param>
 /// <param name="maxCount">
 /// Optional. Your documentation here.
 /// </param>
 /// <returns>
 /// Your documentation here.
 /// </returns>
 public static EventListResponse ExecuteEventQuerySegmented(
     this IFarmOperations operations,
     EventQuery eventQuery,
     EventQueryContinuationToken continuationToken,
     int?maxCount)
 {
     return(operations.ExecuteEventQuerySegmented(eventQuery, continuationToken, maxCount));
 }
 /// <summary>
 /// Your documentation here.  (see
 /// http://msdn.microsoft.com/en-us/library/windowsazure/XXXXX.aspx
 /// for more information)
 /// </summary>
 /// <param name="eventQuery">
 /// Required. Your documentation here.
 /// </param>
 /// <param name="continuationToken">
 /// Optional. Your documentation here.
 /// </param>
 /// <param name="maxCount">
 /// Optional. Your documentation here.
 /// </param>
 /// <returns>
 /// Your documentation here.
 /// </returns>
 public EventListResponse ExecuteEventQuerySegmented(
     EventQuery eventQuery,
     EventQueryContinuationToken continuationToken,
     int?maxCount)
 {
     return
         (Task.Factory.StartNew(
              () => ExecuteEventQuerySegmentedAsync(eventQuery, continuationToken, maxCount, CancellationToken.None))
          .Unwrap().GetAwaiter().GetResult());
 }
        /// <summary>
        /// Your documentation here.  (see
        /// http://msdn.microsoft.com/en-us/library/windowsazure/XXXXX.aspx
        /// for more information)
        /// </summary>
        /// <param name="eventQuery">
        /// Required. Your documentation here.
        /// </param>
        /// <param name="continuationToken">
        /// Optional. Your documentation here.
        /// </param>
        /// <param name="maxCount">
        /// Optional. Your documentation here.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Your documentation here.
        /// </returns>
        public async Task <EventListResponse> ExecuteEventQuerySegmentedAsync(
            EventQuery eventQuery,
            EventQueryContinuationToken continuationToken,
            int?maxCount,
            CancellationToken cancellationToken)
        {
            // Validate
            if (eventQuery == null)
            {
                throw new ArgumentNullException("eventQuery");
            }
            if (maxCount.HasValue && maxCount.Value <= 0)
            {
                throw new ArgumentOutOfRangeException("maxCount");
            }

            var    shouldTrace  = TracingAdapter.IsEnabled;
            string invocationId = String.Empty;

            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                var tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("eventQuery", eventQuery);
                tracingParameters.Add("continuationToken", continuationToken);
                tracingParameters.Add("maxCount", maxCount);
                TracingAdapter.Enter(invocationId, this, "ExecuteEventQuerySegmentedAsync", tracingParameters);
            }
            var response = await QueryEventAsync(eventQuery, continuationToken, maxCount, cancellationToken);

            if (shouldTrace)
            {
                TracingAdapter.Exit(invocationId, response);
            }
            return(response);
        }
        private static async Task <EventListResponse> QueryEventAsync(
            EventQuery eventQuery,
            EventQueryContinuationToken continuationToken,
            int?maxCount,
            CancellationToken cancellationToken)
        {
            if (maxCount.HasValue && maxCount.Value <= 0)
            {
                throw new ArgumentOutOfRangeException("maxCount");
            }
            if (eventQuery == null)
            {
                throw new ArgumentNullException("eventQuery");
            }
            if (eventQuery.TableInfos == null)
            {
                throw new ArgumentException("eventQuery.TableInfos is null");
            }

            var response = new EventListResponse
            {
                Events            = new List <EventModel>(),
                ContinuationToken = new EventQueryContinuationToken()
            };

            var tableinfoIterator = eventQuery.TableInfos.GetEnumerator();

            do
            {
                if (tableinfoIterator.MoveNext())
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    continue;
                }
                response.ContinuationToken = null;
                return(response);
            } while (continuationToken != null && tableinfoIterator.Current.TableName != continuationToken.NextTableName);
            try
            {
                var tableClient = new CloudTableClient(
                    new Uri(eventQuery.TableEndpoint),
                    new StorageCredentials(tableinfoIterator.Current.SasToken));
                var cloudTable = tableClient.GetTableReference(tableinfoIterator.Current.TableName);

                var tableQuery = new TableQuery <DynamicTableEntity>().Where(eventQuery.FilterUri).Take(maxCount);
                cancellationToken.ThrowIfCancellationRequested();
                var tableContinuationToken = continuationToken == null
                    ? null
                    : continuationToken.NextTableContinuationToken;
                var tableResults = await cloudTable.ExecuteQuerySegmentedAsync(tableQuery, tableContinuationToken, cancellationToken);

                response.Events = tableResults.Select(
                    _ => new EventModel
                {
                    Properties = ResolveEventEntity(_)
                }).ToList();
                response.ContinuationToken.NextTableContinuationToken = tableResults.ContinuationToken;
                if (response.ContinuationToken.NextTableContinuationToken == null)
                {
                    if (!tableinfoIterator.MoveNext())
                    {
                        response.ContinuationToken = null;
                        return(response);
                    }
                }
                response.ContinuationToken.NextTableName = tableinfoIterator.Current.TableName;
            }
            catch (StorageException error)
            {
                throw new EventQueryException("Error occurs when query event table", error);
            }
            return(response);
        }