[英]How to get the values from keys in bson document in order to put in a ListBox (C#)
我是 C#/.NET 的新手,我一直在嘗試在字典應用程序中實現數據庫服務器(僅用於學習目的),您可以在其中從列表框中選擇單詞並在文本框中顯示值。 我正在嘗試僅獲取名為“Word”和“Translation”的鍵的列表到字符串
這是 bson(json) 文件
{
"_id": "5ca01f8e36601d012c7b2da1",
"Word": "Bonjour",
"Translation": "Hello",
"Word_Type": "Something",
"Synonyms": "Salut",
"Use_Cases": "Bonjour, comment vas-tu?",
"Definition": "It's a Greeting"
}
這是我用來從數據庫中獲取數據的代碼:
private MongoClient client = new MongoClient("mongodb://localhost:27017");
/// <summary>
/// All the data needed for words
/// </summary>
public class DWord
{
public ObjectId _id { get; set; }
public string Word { get; set; }
public string Translation { get; set; }
public string Word_Type { get; set; }
public string Synonyms { get; set; }
public string Use_Cases { get; set; }
public string Definition { get; set; }
}
private void MethodThatGetsData()
{
var database = client.GetDatabase("DictDB");
var collection = database.GetCollection<BsonDocument>("Words");
var records = collection.Find(new BsonDocument()).ToList();
// I tried using foreach, but I don't know how to get the keys in that bsondocument (called "record" below)
foreach (string record in records)
{
WordsListBox.Items.Add(record);
}
}
我想知道如何獲取每條記錄的鍵值,感謝您的幫助
用這樣的東西替換你的循環:
foreach (BsonDocument record in records)
{
IEnumerable<string> names = record.Names;
// Do something with the names...
}
請注意,通過記錄枚舉為每個查找結果返回一個 BsonDocument。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.