Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DocumentDB显示特定实体类型的所有文档_C#_Asp.net_Azure_Asp.net Core_Azure Cosmosdb - Fatal编程技术网

C# DocumentDB显示特定实体类型的所有文档

C# DocumentDB显示特定实体类型的所有文档,c#,asp.net,azure,asp.net-core,azure-cosmosdb,C#,Asp.net,Azure,Asp.net Core,Azure Cosmosdb,我有一个通用的IDocumentDbRepository存储库,用于为DocumentDB提供基本CRUD操作,并为特定实体的其他操作提供特定存储库,这些存储库继承或实现此接口和类 public interface IDocumentDbRepository<T> where T : class { //IEnumerable<T> GetItems(); Task<IEnumerable<T>> GetItemsAsync

我有一个通用的
IDocumentDbRepository
存储库,用于为DocumentDB提供基本CRUD操作,并为特定实体的其他操作提供特定存储库,这些存储库继承或实现此接口和类

    public interface IDocumentDbRepository<T> where T : class
{
    //IEnumerable<T> GetItems();
    Task<IEnumerable<T>> GetItemsAsync();
    Task<T> GetItemAsync(T id);
    Task<T> AddDocumentAsync(T item);
    Task<T> UpdateDocumentAsync(T id, T item);
    Task DeletedocumentAsync(T id);
}

public class DocumentDbRepository<T> : IDocumentDbRepository<T> where T : class
{
    private readonly string AuthKey;
    private readonly string EndpointUri;
    private readonly string DatabaseId;
    private readonly string CollectionId;
    private static DocumentClient client;


    public DocumentDbRepository(IOptions<DocumentDbSettings> DocumentDbConfig)
    {
        this.DatabaseId = DocumentDbConfig.Value.DatabaseName;
        this.CollectionId = DocumentDbConfig.Value.CollectionName;
        this.AuthKey = DocumentDbConfig.Value.AuthKey;
        this.EndpointUri = DocumentDbConfig.Value.EndpointUri;
        client = new DocumentClient(new Uri(EndpointUri), AuthKey);
    }

public async Task<IEnumerable<T>> GetItemsAsync()
{
     IDocumentQuery<T> query = client.CreateDocumentQuery<T>(
     UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId))
     .AsDocumentQuery();

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

   // public IEnumerable<T> GetItems()
   // {
   //     var results = client.CreateDocumentQuery<T>//(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId)).ToList();
   //   return results;
   // }
//methods
我在控制器中调用
GetAllEmployees
方法,仅列出视图中类型为
Employee
的文档,但它不起作用,并且列出了具有任何实体类型的所有文档。我做错了什么

public IActionResult Index()
{
    return View(_employeeRepository.GetAllEmployees());
}
好的,有几件事

首先,永远不要调用
.ToList()CreateDocumentQuery
上执行code>。它将通过网络进行多个同步调用,以返回数据库中的每个文档。这将导致疯狂的RU/s和非常糟糕的性能

使用
AsDocumentQuery
并在
query.HasMoreResults
时调用
ExecuteNextAsync

第二,当您调用
ToList()
方法时,所有内容都将作为员工返回,因为您是以这种方式查询的。你基本上是说,“把这个集合中的所有东西都作为一名员工交给我,然后再把员工交给我”。在这个阶段,他们都是雇员

您真正需要的是文档上的某种
类型
属性,您可以在执行
ToList
调用之前使用该属性进行查询

我建议您检查一下在中的工作方式。听起来正是你需要的

是如何在不阻塞线程的情况下异步调用ToList方法


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

感谢您的回答,我将利用您的提示。该属性类型。你的意思是我应该在我的
员工
模型和我的
员工档案
中创建类似于
公共bool isEmployee
的属性,我将查询只返回
isEmployee==true
的实体。如果它是一个布尔值,那么您只能使用两种不同的类型。只需创建一个名为
EntityType
的属性接口,并让DTO实现它。然后你可以做一个
Where(x=>x.EntityType==nameof(Employee)).toListSync()
来查询你需要什么。我编辑了我的问题,并根据你的建议创建了
GetItemsAsync()
方法?还创建了接口,该接口现在由my
Employee
模型实现,它现在具有
EntityType
属性。我只想从我的
员工档案中的
GetAllEmployees()
返回员工实体,但是尽管你的建议我有点不知所措,你能提供一些代码吗?抱歉,我是CosmosDB的新手。您必须将
where T:class
更改为
where T:IYourInterface
,然后您应该能够执行类似这样的操作
IDocumentQuery=client.CreateDocumentQuery(UriFactory.CreateDocumentCollectionUri(DatabaseId,CollectionId))。其中(x=>x.EntityType==nameof(T)).AsDocumentQuery();
您的意思是更改
DocumentDBRepository
中的T:IYourInterface
?这会有什么帮助,这可能吗?。我想在我的
EmployeeRepository
中重用通用的
GetItemsAsync()
方法,并基于
GetItemsAsync()
方法已经接收到的项目在某些方法中进行查询
public IActionResult Index()
{
    return View(_employeeRepository.GetAllEmployees());
}