C# 如何通过webapi从DocumentDB正确获取所有项目

C# 如何通过webapi从DocumentDB正确获取所有项目,c#,.net,asp.net-web-api,azure-cosmosdb,C#,.net,Asp.net Web Api,Azure Cosmosdb,我有以下DocumentDB存储库帮助器类 using Microsoft.Azure.Documents; using Microsoft.Azure.Documents.Client; using Microsoft.Azure.Documents.Linq; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Linq.Expr

我有以下DocumentDB存储库帮助器类

using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Web;

namespace TenantManagementWebApi.DataAccess
{
    public static class DocumentDBRepository<T> where T : class
    {
        private static readonly string DatabaseId = ConfigurationManager.AppSettings["database"];
        private static readonly string CollectionId = ConfigurationManager.AppSettings["collection"];
        private static DocumentClient client;

        public static async Task<T> GetItemAsync(string id)
        {
            try
            {
                Document document = await client.ReadDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id));
                return (T)(dynamic)document;
            }
            catch (DocumentClientException e)
            {
                if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    return null;
                }
                else
                {
                    throw;
                }
            }
        }

        public static async Task<IEnumerable<T>> GetItemsAsync(Expression<Func<T, bool>> predicate)
        {
            IDocumentQuery<T> query = client.CreateDocumentQuery<T>(
                UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId),
                new FeedOptions { MaxItemCount = -1 })
                .Where(predicate)
                .AsDocumentQuery();

            List<T> results = new List<T>();
            while (query.HasMoreResults)
            {
                results.AddRange(await query.ExecuteNextAsync<T>());
            }

            return results;
        }

        public static async Task<Document> CreateItemAsync(T item)
        {
            return await client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId), item);
        }

        public static async Task<Document> UpdateItemAsync(string id, T item)
        {
            return await client.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id), item);
        }

        public static async Task DeleteItemAsync(string id)
        {
            await client.DeleteDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id));
        }

        public static void Initialize()
        {
            client = new DocumentClient(new Uri(ConfigurationManager.AppSettings["endpoint"]), ConfigurationManager.AppSettings["authKey"]);
            CreateDatabaseIfNotExistsAsync().Wait();
            CreateCollectionIfNotExistsAsync().Wait();
        }

        private static async Task CreateDatabaseIfNotExistsAsync()
        {
            try
            {
                await client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(DatabaseId));
            }
            catch (DocumentClientException e)
            {
                if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    await client.CreateDatabaseAsync(new Database { Id = DatabaseId });
                }
                else
                {
                    throw;
                }
            }
        }

        private static async Task CreateCollectionIfNotExistsAsync()
        {
            try
            {
                await client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId));
            }
            catch (DocumentClientException e)
            {
                if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    await client.CreateDocumentCollectionAsync(
                        UriFactory.CreateDatabaseUri(DatabaseId),
                        new DocumentCollection { Id = CollectionId },
                        new RequestOptions { OfferThroughput = 1000 });
                }
                else
                {
                    throw;
                }
            }
        }
    }
}
我不确定在谓词中放入什么来获取所有租户项

 public async Task<List<Tenant>> GetTenants()
        {
            return await  DocumentDBRepository<List<Tenant>>.GetItemsAsync(d => d. != null);
        }
public异步任务GetTenants()
{
return wait DocumentDBRepository.GetItemsAsync(d=>d.!=null);
}

您需要使通用
T
对象更具体。将其限制为实现某种类型接口的类,如
icosmentity
,并在该接口上添加
EntityType
属性

然后让DTO实现该接口,并将
EntityType
设置为类名。这样,您就可以创建一个动态谓词,该谓词获取
nameof(T)
,并自动将其添加到LINQ查询的
Where
子句中

如果您只想获取集合中的所有项,那么在代码中,
x=>true
谓词将执行此操作,但它不会将其限制在租户中,除非集合中只有租户对象

另外,
readdocumentsync
只有在您的收藏没有分区键时才值得这样做(实际上不建议这样做)

它可能值得一看,因为它正是你想要的,甚至更多。它支持与您试图编写代码的逻辑完全相同的逻辑


免责声明,我是宇航员的创造者。

你能提供更多关于答案的信息吗(用代码),我很困惑,我读了你的github,稍后我会尝试一下,非常令人印象深刻,但现在我需要解决这个问题,我昨天回答了同样的问题。安装程序包可能会有帮助:无法安装程序包“Cosmonaut 1.4.7”。您正试图将此程序包安装到以“.NETFramework,Version=v4.6”为目标的项目中,但该程序包不包含任何与该框架兼容的程序集引用或内容文件。有关更多信息,请联系软件包作者。
 public async Task<List<Tenant>> GetTenants()
        {
            return await  DocumentDBRepository<List<Tenant>>.GetItemsAsync(d => d. != null);
        }