简体   繁体   English

如何使用 MongoDb C# Driver 2.1 异步使用 AsQueryable 方法?

[英]How do I use the AsQueryable method asynchronously with MongoDb C# Driver 2.1?

The release of version 2.1 of the MongoDb C# Driver has recently reintroduced the method AsQueryable , but I am struggling to find a way of calling it asynchronously. MongoDb C# 驱动程序的 2.1 版本最近重新引入了AsQueryable方法,但我正在努力寻找一种异步调用它的方法。

With Entity Framework this would be achieved using QueryableExtensions.ToListAsync but I can't see an equivalent using MongoDb.使用 Entity Framework,这将使用QueryableExtensions.ToListAsync来实现,但我看不到使用 MongoDb 的等效项。

So given a repository method such as:因此,给定一个存储库方法,例如:

public IQueryable<MyType> GetFiltered(Expression<Func<MyType, bool>> predicate)
{
        return Database.GetCollection<MyType>(typeof(MyType).Name).AsQueryable().Where(predicate);
}

I wanted to do something like我想做类似的事情

var myTypes = await MyRepository.GetFiltered(t => t.Id == 1).ToListAsync();

Is this possible?这可能吗?

You're returning the wrong type from the GetFiltered function.您从GetFiltered函数返回了错误的类型。 It should be returning an IMongoQueryable<MyType> instead of IQueryable<MyType> :它应该返回IMongoQueryable<MyType>而不是IQueryable<MyType>

public IMongoQueryable<MyType> GetFiltered(Expression<Func<MyType, bool>> predicate)
{
    return Database.GetCollection<MyType>(typeof(MyType).Name).AsQueryable()
        .Where(predicate);
}

You can then successfully call it as:然后您可以成功地将其称为:

var myTypes = await MyRepository.GetFiltered(t => t.Id == 1).ToListAsync();

I up-voted the accepted answer.我对接受的答案投了赞成票。

If you need to abstract away the IMongoQueryable interface from the caller this little extension helper may be useful.如果您需要从调用者那里抽象出IMongoQueryable接口,这个小扩展助手可能会很有用。

public static class MongoQueryableMixIn
{
    public static Task<List<T>> ToMongoListAsync<T>(this IQueryable<T> mongoQueryOnly)
    {
        return ((IMongoQueryable<T>) mongoQueryOnly).ToListAsync();
    }
}

At first you must call ToCursorAsync() for IMonqoQuarable<T> object, and then call ToListAsync() for the awaited IAsyncCursor result:起初,你必须调用ToCursorAsync()IMonqoQuarable<T>对象,然后调用ToListAsync()为等待IAsyncCursor结果:

public IMongoQueryable<MyType> GetFiltered(Expression<Func<MyType, bool>> predicate)
{
    return Database.GetCollection<MyType>(typeof(MyType).Name).AsQueryable()
        .Where(predicate);
}

And then进而

var myTypes = await(await MyRepository.GetFiltered(t => t.Id == 1).ToCursorAsync()).ToListAsync();

I am posting this later as an update for the newer versions of MongoDB driver.我稍后会将此作为更新版本的 MongoDB 驱动程序的更新发布。

In the newer versions of MongoDB, you can call it asynchronously.在较新版本的 MongoDB 中,您可以异步调用它。 You need to include the MongoDB.Driver.Linq:您需要包含 MongoDB.Driver.Linq:

using MongoDB.Driver.Linq;
var myTypes = await Database.GetCollection<MyType>().AsQueryable()
.Where(t => t.Id == 1).ToListAsync();

As JohnnyHK mentioned in the accepted answer, the return type is incorrect and should be IMongoQueryable<T> , however, this is not all that is required.正如 JohnnyHK 在接受的答案中提到的那样,返回类型不正确,应该是IMongoQueryable<T> ,但是,这并不是所需要的全部。

To do the.ToListAsync() on a IMongoQueryable<T> object, you also need to include the namespace using MongoDB.Driver.Linq .要在IMongoQueryable<T> object 上执行.ToListAsync(),您还需要using MongoDB.Driver.Linq包含命名空间。 Now this is a no-brainer if your IMongoQueryable<T> interface provider is in the same file as where you want to call the.ToListAsync(), but in my case, a base class supplies the IMongoQueryable<T> interface, and I want to use it in my derived class.现在,如果您的IMongoQueryable<T>接口提供程序与您要调用 .ToListAsync() 的位置位于同一个文件中,那么这很简单,但在我的例子中,基础 class 提供了IMongoQueryable<T>接口,我想在我派生的 class 中使用它。

I needed to add the using MongoDB.Driver.Linq to get access to that extension method.我需要添加using MongoDB.Driver.Linq才能访问该扩展方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM