我正在尝试通过过滤器获取文档列表,该列表返回了categoryId = 1的帖子。我不知道如何使这项工作:
public List<Post> getAllProgramming()
{
var result = new List<Post>();
var mongoClient = new MongoClient("mongodb://localhost");
var database = mongoClient.GetDatabase("SearchForKnowledge");
var coll = database.GetCollection<BsonDocument>("Posts");
var filter = Builders<BsonDocument>.Filter.Eq("CategoryId", 1);
result = coll.Find(filter).ToList<Post>;
}
一切都很好,除了代码的最后一行,转换无效。应该解决这个问题的原因是什么?提前谢谢你,周五过得愉快!
答案 0 :(得分:0)
你可以尝试以下......
public List<Post> getAllProgramming()
{
var result = new List<Post>();
var mongoClient = new MongoClient("mongodb://localhost");
var database = mongoClient.GetDatabase("SearchForKnowledge");
var coll = database.GetCollection<Post>("Posts");
var filter = Builders<Post>.Filter.Eq(p => p.CategoryId, 1);
result = coll.Find(filter).ToList();
}