Example #1
0
        private Task <T> SingleOrDefaultAsync <T>(
            IDocumentQuery <T> offerQuery,
            CancellationToken cancellationToken)
        {
            if (offerQuery.HasMoreResults)
            {
                return(offerQuery.ExecuteNextAsync <T>(cancellationToken)
                       .ContinueWith(nextAsyncTask =>
                {
                    DocumentFeedResponse <T> offerFeedResponse = nextAsyncTask.Result;
                    if (offerFeedResponse.Any())
                    {
                        return Task.FromResult(offerFeedResponse.Single());
                    }

                    return SingleOrDefaultAsync(offerQuery, cancellationToken);
                })
                       .Unwrap());
            }

            return(Task.FromResult(default(T)));
        }
Example #2
0
        //Helper to materialize Any IResourceFeed<T> from IResourceFeed<dynamic> as long as source
        //conversion from dynamic to T.

        //This method is invoked via expression as part of dynamic binding of cast operator.
        public static DocumentFeedResponse <T> Convert <T>(DocumentFeedResponse <dynamic> dynamicFeed)
        {
            if (typeof(T) == typeof(object))
            {
                return((DocumentFeedResponse <T>)(object) dynamicFeed);
            }
            IList <T> result = new List <T>();

            foreach (T item in dynamicFeed)
            {
                result.Add(item);
            }

            return(new DocumentFeedResponse <T>(
                       result,
                       dynamicFeed.Count,
                       dynamicFeed.Headers,
                       dynamicFeed.UseETagAsContinuation,
                       dynamicFeed.QueryMetrics,
                       dynamicFeed.RequestStatistics,
                       responseLengthBytes: dynamicFeed.ResponseLengthBytes));
        }