2016-05-06 144 views
0

我想通過篩選器獲取文檔列表,該篩選器返回具有categoryId = 1的帖子。我不知道如何使這項工作:MongoDb c#將文檔轉換爲對象

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

您可以嘗試如下...

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