/// <summary>
        ///
        /// </summary>
        /// <param name="id"></param>
        /// <returns>Document</returns>
        public static async Task <Document> GetTodoItemById(Guid id)
        {
            //  Best way if selfLink is not available...

            IDocumentQuery <Document> query = (
                from doc in client.CreateDocumentQuery(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId))
                where doc.Id.Equals(id.ToString())
                select doc
                ).AsDocumentQuery();

            Document documentToReturn = null;

            while (query.HasMoreResults)
            {
                FeedResponse <Document> res = await query.ExecuteNextAsync <Document>();

                if (res.Any())
                {
                    documentToReturn = res.Single();
                    break;
                }
            }

            return(documentToReturn);

            //FeedOptions queryOptions = new FeedOptions { MaxItemCount = 1 };

            // Find by Id

            //IQueryable<TodoItem> todoQuery = client.CreateDocumentQuery<TodoItem>(
            //    UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId), queryOptions)
            //    .Where(todo => todo.Id.Equals(id.ToString())).FirstOrDefault();

            //FeedResponse<TodoItem> feedResponse =

            //IDocumentQuery<TodoItem> query = await client.CreateDocumentQuery<TodoItem>(
            //    UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId), queryOptions)
            //    .Where(todo => todo.Id.Equals(id.ToString())).AsDocumentQuery();

            //using (var todoQuery = client.CreateDocumentQuery<TodoItem>(
            //    UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId), queryOptions)
            //    .Where(todo => todo.Id.Equals(id.ToString())).FirstOrDefault()
            //    .AsDocumentQuery().ExecuteNextAsync();
            //{
            //    return await todoQuery.ExecuteNextAsync().Result;
            //}
        }
示例#2
0
        private static async Task ValidateQueryResultsAsync(
            Container container,
            string query                    = null,
            TestDoc expectedDoc             = null,
            QueryDefinition queryDefinition = null)
        {
            QueryRequestOptions requestOptions = expectedDoc != null
                ? new QueryRequestOptions()
            {
                PartitionKey = new PartitionKey(expectedDoc.PK),
            }
                : null;

            FeedIterator <TestDoc> queryResponseIterator;

            if (query != null)
            {
                queryResponseIterator = container.GetItemQueryIterator <TestDoc>(query, requestOptions: requestOptions);
            }
            else
            {
                queryResponseIterator = container.GetItemQueryIterator <TestDoc>(queryDefinition, requestOptions: requestOptions);
            }

            FeedResponse <TestDoc> readDocs = await queryResponseIterator.ReadNextAsync();

            Assert.AreEqual(null, readDocs.ContinuationToken);

            if (expectedDoc != null)
            {
                Assert.AreEqual(1, readDocs.Count);
                TestDoc readDoc = readDocs.Single();
                Assert.AreEqual(expectedDoc, readDoc);
            }
            else
            {
                Assert.AreEqual(0, readDocs.Count);
            }
        }