First of all I\'m new to MongoDb. In MongoDb C# driver 1.9.x, i can take collections as queryable with AsQueryable method like this.
var db = client.Get
While other answers indicated that the early version 2 releases of the drivers didn't include the AsQueryable
this is now available in the latest version of the drivers (I've not checked exactly which version introduced it).
The method is found in MongoDB.Driver.IMongoCollectionExtensions
and can be invoked as you would expect. That is:
IMongoCollection<TDocument> collection = ...;
IMongoQueryable<TDocument> queryable = collection.AsQueryable();
I originally had the following using mongocsharp version 1.9x:
public IQueryable<T> SearchFor(Expression<Func<T, bool>> predicate)
{
return _collection.AsQueryable<T>()
.Where(predicate.Compile()).AsQueryable();
}
Was able to get the same results in version 2 using:
public async Task<List<T>> SearchFor(Expression<Func<T, bool>> predicate)
{
return await _collection.Find(Builders<T>.Filter.Where(predicate)).ToListAsync();
}
Hope it helps.
19 October update:
MongoDB 2.1 driver is out https://github.com/mongodb/mongo-csharp-driver/releases/tag/v2.1.0
It supports LINQ:
LINQ
CSHARP-935 LINQ support has been rewritten and now targets the aggregation framework. It is a more natural translation and enables many features of LINQ that were previously not able to be translated.
Simply use the new AsQueryable method to work with LINQ.
18 September update:
MongoDB 2.1 driver RC should support it. See https://jira.mongodb.org/browse/CSHARP-935
Finally 2.1 rc came out. Great work!
Old answer:
No, AsQueryable is unsupported: https://jira.mongodb.org/browse/CSHARP-935
Type: Epic Status:OPEN Priority: Major - P3 Resolution: Unresolved
And from the hourse's mouth: Craig Wilson on the google forum
Yes, there is currently no AsQueryable on the new api. You can track this feature here (https://jira.mongodb.org/browse/CSHARP-935). We simply didn't have enough time to get it completed and tested thoroughly. It is scheduled for 2.1 and is a priority for us. Until then, we have integrated expression tree functionality into the Find and Aggregate methods (and some of the write methods for filtering) such that you may not need a full LINQ implementation. For instance, see the sample test class here as an example: https://github.com/mongodb/mongo-csharp-driver/blob/master/src/MongoDB.Driver.Tests/Samples/AggregationSample.cs#L77