获取对象列表 mongodb driver net core

Get List of object mongodb driver net core

您好,我需要从 mongodb 获取对象列表,我正在使用 mongodb.driver 和网络核心:

我遇到的错误是:

Error CS0266 Cannot implicitly convert type 'MongoDB.Driver.IAsyncCursor>' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?) ...\Implementations\EventRepository.cs 104 Active

我如何才能 return 一个 IEnumerable 正确,这些是我的代码:

public async Task<IEnumerable<Event>> GetEventsByOwnerId(IDictionary<string, string> paramsEvents)
{
    try
    {
        FilterDefinition<Event> filter = Builders<Event>.Filter.Eq("OwnerId", paramsEvents["OwnerId"]);
        var response = await this.GetMongoCollection().FindAsync<Event>(filter);

        return response;
    }
    catch (MongoException e)
    {
        throw new MongoException(e.ToString());
    }
    catch (Exception e)
    {
        throw new Exception(e.ToString());
    }
}

FindAsync method returns IAsyncCursor 没有实现 IEnumerable<T> 所以这里没有可以是 运行 的隐式转换。您必须使用 .ToList() 到 "materialize" 查询(获取数据):

var response = await this.GetMongoCollection().FindAsync<Event>(filter);
return response.ToList();