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.
Why can't linq find an implementation for mongodb?
-
16-06-2023 - |
문제
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:
- A using directive for MongoDB.Driver.Linq
- A class with a few fields, inheriting another class which just provides the standard ObjectId Id field.
- A reference to a collection of said class of type MongoCollection obtained Connection.GetCollection
- 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;
}
}
}
해결책 2
다른 팁
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;
제휴하지 않습니다 StackOverflow