简体   繁体   中英

Why can't linq find an implementation for mongodb?

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;
        }
    }
}

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;

为了找到Linq提供程序,不仅必须具有该提供程序的using语句,而且还必须具有System.Linq本身。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM