33

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?

0

5 Answers 5

40
+50

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();
16

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();
0
6

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();
    }
}
3
  • 2
    Kinda surprised this isn't built-in.
    – Konrad
    Mar 24, 2021 at 14:04
  • 3
    I just realized I wasn't using async methods with AsQueryable with MongoDB because .Where returns IQueryable. Entity framework provides EntityFrameworkQueryableExtensions class with stuff like that.
    – Konrad
    Mar 24, 2021 at 14:05
  • 2
    I just found there is Find method that returns IFindFluent and you can use it instead of AsQueryable+Where
    – Konrad
    Mar 24, 2021 at 14:18
1

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();
0

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.

To do the .ToListAsync() on a IMongoQueryable<T> object, you also need to include the namespace 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.

I needed to add the using MongoDB.Driver.Linq to get access to that extension method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.