0

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;
        }
    }
}
Ludeme Games
  • 69
  • 1
  • 5

2 Answers2

0

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;
har07
  • 88,338
  • 12
  • 84
  • 137
  • The documentation I linked to suggests that the document type be the generic parameter, not the collection. I have tried both per your suggestion and neither compile. Please see the recently added code sample above for more info. – Ludeme Games Mar 19 '14 at 06:06
  • with that code, are you still getting the same error message "*Could not find an implementation of the query pattern...*" ? or different error message now? – har07 Mar 19 '14 at 06:11
  • Same error. Here is the entire error - "Could not find an implementation of the query pattern for source type 'System.Linq.IQueryable'. 'Where' not found. Are you missing a reference or using directive for 'System.Linq'? – Ludeme Games Mar 19 '14 at 06:16
  • Hmm... here's a question. I may misunderstand how Linq providers work. If I add a reference to System.Linq, will it use the default linq implementation (bad) or is a reference to System.Linq compulsory for getting C# to use MongoDB.Driver.Linq? (Note: Adding the reference to System.Linq causes it to compile, but as I understand it if the default Linq implementation is used on the database it will lead to severe performance problems) – Ludeme Games Mar 19 '14 at 06:20
  • 1
    I'm not experienced with mongodb, but I think it is the latter. Native `Linq` won't be able to automatically deal with any kind of database (MongoDB in this case). If `System.Linq` works, it must be using MongoDB driver somehow. – har07 Mar 19 '14 at 06:24
  • Yes. Missing either using directive fails to compile. If you put this information into a different answer I will accept it to your credit. – Ludeme Games Mar 19 '14 at 06:26
0

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.

Ludeme Games
  • 69
  • 1
  • 5