【发布时间】:2017-01-11 22:49:08
【问题描述】:
我的 MongoDB 中有数据,其中存储了一个可为空的 int,通常为 null。我在 C# 端的模型的变量为 int? MaterialID。
当我运行.Query.FirstOrDefault() 时,我收到错误消息“输入字符串的格式不正确。”每当它尝试从 Mongo 读取空值时(整数工作正常)。
我设法通过改变我的 int 来解决这个问题?到一个字符串,然后再将其转换回一个 int?,但这是一个我真的不喜欢的可怕的 hack。有人对我如何将 Mongo 中的 null 读入我的 int 有任何想法吗?在 C# 方面?
谢谢。
我的 JSON:
"Collection" : {
"List" : [
{
"ID" : "40",
"StartDate" : ISODate("2013-01-01T00:00:00.000Z"),
"EndDate" : ISODate("2013-12-31T00:00:00.000Z"),
"MaterialID" : "3"
},
{
"ID" : "40",
"StartDate" : ISODate("2014-01-01T00:00:00.000Z"),
"EndDate" : ISODate("2014-09-30T00:00:00.000Z"),
"MaterialID" : "null"
}
]
【问题讨论】:
-
你能发布你的对象的 json 吗? (使用非空 MaterialId)
-
@SergeyBerezovskiy,你明白了
-
所以你的文档没有_id字段?
标签: c# .net mongodb mongodb-.net-driver