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.
With Entity Framework this would be achieved using QueryableExtensions.ToListAsync
but I can't see an equivalent using 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. It should be returning an IMongoQueryable<MyType>
instead of 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 am posting this later as an update for the newer versions of MongoDB driver.
In the newer versions of MongoDB, you can call it asynchronously. You need to include the MongoDB.Driver.Linq:
using MongoDB.Driver.Linq;
var myTypes = await Database.GetCollection<MyType>().AsQueryable()
.Where(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.
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:
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With