BsonElement 到 c# 数据类型
我的数据库中有一个“BSonElement”,我已使用标准查询重试它。
问题是我无法将 BsonDocument 转换为类型。
示例:
更新 1:
public partial class item_Stat
{
[BsonExtraElements]
public BsonDocument all_stat;
}
基本上,我已经进入了我可以使用“BsonExtraElements”读取的 DB 10-15 属性(字段)。这样,我可以重试属性而不定义它 在 C# 中。
all_stat,可以具有动态变化的 10-15-20 属性。 C# 是类型化语言,因此我无法在 C# 中定义此属性,并且我使用了 ExtraElements。
问题是当我从数据库查询对象时。
var item_db = myMongoCollection.find(theQuery); // find the OBJECT
item_db.all_stat // all the property hare HERE
// find the property "category_01"
var i = item_db.all_stat.Where(p => p.Name == "category_01").Single();
// ok, i have found the Category, so i can cast it to C# Data Type
var typed_value = (ItemStatSingle) i.Value // BsonElement to ItemStatSingle
I have an "BSonElement" in my DB and i have retry it with an standard Query.
The problem was that i can't Cast BsonDocument to Type.
Example:
UPDATE 1:
public partial class item_Stat
{
[BsonExtraElements]
public BsonDocument all_stat;
}
Basically, i have into my DB 10-15 property (field) that I can read with "BsonExtraElements". In this way, i can retry property without define it
in C#.
all_stat, can have 10-15-20 property that dinamically change.
C# is typed language, so I can't define this property in C# and i have used ExtraElements.
The problem is when i QUERY the Object from DB.
var item_db = myMongoCollection.find(theQuery); // find the OBJECT
item_db.all_stat // all the property hare HERE
// find the property "category_01"
var i = item_db.all_stat.Where(p => p.Name == "category_01").Single();
// ok, i have found the Category, so i can cast it to C# Data Type
var typed_value = (ItemStatSingle) i.Value // BsonElement to ItemStatSingle
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
BsonElement.Value 是 BsonValue 类型。使用 As* 方法之一进行适当转换。这里的值是什么类型?由于您有用户定义的类型,因此您最好的选择是像 Barrie 上面所说的那样进行检索。如果您想自定义“映射”,请参阅序列化教程 http:// /www.mongodb.org/display/DOCS/CSharp+Driver+Serialization+Tutorial
BsonElement.Value is of type BsonValue. Use one of the As* method to convert appropriately. What is the type of the value here? Since you have a user defined type your best option is to retrieve as Barrie says above. If you want to customize the "mapping" refer to the Serialization tutorial http://www.mongodb.org/display/DOCS/CSharp+Driver+Serialization+Tutorial
以下是您可以执行的操作示例,给定域模型中的类,如下所示:
您可以像这样使用您的类:
Here's an example of what you can do, given a class from your domain model like:
You can use your class like this:
您不能只从额外的元素中转换 BsonDocument。你必须反序列化它。
假设您有一个类 C
和一个 extraDocuments 变量(类似于您的 item_db.all_stat 属性),初始化如下:
然后您可以提取“c”值并像这样反序列化它:
You can't just cast a BsonDocument from your extra elements. You have to Deserialize it.
Suppose you have a class C
and an extraDocuments variable (analogous to your item_db.all_stat property) initialized like this:
Then you could extract the "c" value and deserialize it like this: