Question

I am trying to use linq in C# with mongodb per the tutorial here http://docs.mongodb.org/ecosystem/tutorial/use-linq-queries-with-csharp-driver/

I have:

  1. A using directive for MongoDB.Driver.Linq
  2. A class with a few fields, inheriting another class which just provides the standard ObjectId Id field.
  3. A reference to a collection of said class of type MongoCollection obtained Connection.GetCollection
  4. Lastly, my query - from item in MyCollection.AsQueryable() select item;

The compiler complains that Error: Could not find an implementation of the query pattern...

What is missing?

---EDIT---

I have minimized the code here to illustrate the problem -

// .Net
using System.Collections.Generic;

// 3rd Party
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Driver.Linq;

namespace Chess2.Server {
    public static partial class Database {
        internal static MongoCollection<Document> Collection =
            GetCollection<Document>();
    }
    public class Document {
        public ObjectId Id;
        public int Field;

        public static IEnumerable<Document> Waiting() {
            // HERE IS THE LINQ THAT DOESN"T WORK
            return from item in
                       Database.Collection.AsQueryable<Document>()
                   where item.Field > 0
                   select item;
        }
    }
}
Was it helpful?

Solution 2

In order for a Linq provider to be found, one must not only have the using statement for the provider, but also System.Linq itself.

OTHER TIPS

You're missing queryable type, at least that's what I think after looking at the tutorial link. If it is querying against MongoCollection, I think your query should've been :

var query = from item in MyCollection.AsQueryable<MongoCollection>() select item;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top