Ejemplo n.º 1
0
        private async Task <T> GetSystemDocumentByIdAsync <T>(string id, string partitionKey, CancellationToken cancellationToken)
        {
            EnsureArg.IsNotNull(id, nameof(id));
            EnsureArg.IsNotNull(partitionKey, nameof(partitionKey));

            var documentQuery = new SqlQuerySpec(
                "SELECT * FROM root r WHERE r.id = @id",
                new SqlParameterCollection(new[] { new SqlParameter("@id", id) }));

            var feedOptions = new FeedOptions
            {
                PartitionKey = new PartitionKey(partitionKey),
            };

            IDocumentQuery <T> cosmosDocumentQuery =
                CreateDocumentQuery <T>(documentQuery, feedOptions);

            using (cosmosDocumentQuery)
            {
                FeedResponse <dynamic> response = await cosmosDocumentQuery.ExecuteNextAsync(cancellationToken);

                var result = response.SingleOrDefault();

                if (result == null)
                {
                    _logger.LogError($"{typeof(T)} with id {id} was not found in Cosmos DB.");
                }

                return(result);
            }
        }
Ejemplo n.º 2
0
        public static async Task <T> SingleOrDefault <T>(this IQueryable <T> query)
        {
            FeedResponse <T> response = await
                                        query.AsDocumentQuery().ExecuteNextAsync <T>();

            return(response.SingleOrDefault());
        }