【问题标题】:c# and mongoDB - Cannot deserialize from BsonType Nullc# 和 mongoDB - 无法从 BsonType Null 反序列化
【发布时间】:2016-12-09 18:36:14
【问题描述】:

我是 MongoDB 的新手,我一直在认真阅读 MongoDB 2.2 初学者指南和官方 C# 驱动程序http://www.codeproject.com/Articles/524602/Beginners-guide-to-using-MongoDB-and-the-offic

一切似乎都很顺利,除了我不确定如何处理 MongoDB Null 值

这是我的数据结构

public class DataStructure
{
    public ObjectId _id { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime UpdatedOn { get; set; }
    public string ClaimId { get; set; }
    public string Status { get; set; }
    public string FmcCity { get; set; }
    public string FmcState { get; set; }
    public Int32 TestType { get; set; }
    public List<LineItemStructure> LineItems { get; set; }
}

public class LineItemStructure
{
    public string LineItemDescription { get; set; }
    public string LineItemType { get; set; }
    public string PartNumber { get; set; }
    public double LaborHours { get; set; }
    public double Quantity { get; set; }
}

这是连接到MongoDB并拉回值的代码,直到遇到空字段值并返回以下错误

       var client = new MongoClient(connectionString);
       MongoServer server = client.GetServer();
       var database = server.GetDatabase("FleetClaims");
      MongoCollection< DataStructure> collection = database.GetCollection< DataStructure >("Claims");
        //Execute the query
        MongoCursor< DataStructure> results = collection.FindAll();

"反序列化类的 LineItems 属性时出错 ...:反序列化 LaborHours 属性时发生错误 class ... : 无法从 BsonType Null 反序列化 Double。”

谁能建议一个参考或不同的方法来处理空值? BsonString.Empty 会帮我吗?

【问题讨论】:

    标签: c# mongodb serialization


    【解决方案1】:
    public double? LaborHours { get; set; }
    public double? Quantity { get; set; }
    

    允许我将空值传递到这些字段中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-08
      • 2015-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多