简体   繁体   English

c#和mongoDB-无法从BsonType Null反序列化

[英]c# and mongoDB - Cannot deserialize from BsonType Null

I'm new to MongoDB and I've been religiously slogging through the Beginners' guide to using MongoDB 2.2 and the official C# driver http://www.codeproject.com/Articles/524602/Beginners-guide-to-using-MongoDB-and-the-offic 我是MongoDB的新手,我一直在虔诚地搜寻着使用MongoDB 2.2的初学者指南和官方C#驱动程序http://www.codeproject.com/Articles/524602/Beginners-guide-to-using-MongoDB -和最OFFIC

Everything seems to be going well, except I'm not sure how to handle a MongoDB Null value 一切似乎都进行得很好,但我不确定如何处理MongoDB Null值

This is my data structure 这是我的数据结构

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

And this is the code which connects to the MongoDB and pulls back values, until it encounters a null field value and returns the error below 这是连接到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();

"An error occurred while deserializing the LineItems property of class ...: An error occurred while deserializing the LaborHours property of class ... : Cannot deserialize Double from BsonType Null." “反序列化类...的LineItems属性时发生错误:反序列化类...的LaborHours属性时发生错误:无法从BsonType Null反序列化Double。”

Can anyone suggest a reference or different approach to handle the nulls? 谁能建议参考或其他方法来处理空值? Will BsonString.Empty help me here? BsonString.Empty可以在这里帮助我吗?

public double? LaborHours { get; set; }
public double? Quantity { get; set; }

Allowed me to pass nulls into those fields. 请允许我将空值传递到这些字段中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MongoDB C#2驱动程序—无法从BsonType&#39;Double&#39;反序列化&#39;String&#39; - MongoDB C# 2 Driver — Cannot deserialize 'String' from BsonType 'Double' 无法从MongoDb C#中的BsonType ObjectId反序列化字符串# - Cannot deserialize string from BsonType ObjectId in MongoDb C# MongoDB C#:无法从BsonType&#39;DateTime&#39;反序列化&#39;String&#39; - MongoDB C#: Cannot deserialize a 'String' from BsonType 'DateTime' mongodb C#异常无法从BsonType Int32反序列化字符串 - mongodb C# exception Cannot deserialize string from BsonType Int32 C#Mongo驱动程序错误:无法从BsonType&#39;Document&#39;反序列化&#39;Int32&#39; - C# Mongo Driver error: Cannot deserialize a 'Int32' from BsonType 'Document' .NET MongoDB:从BsonType数组反序列化BsonDocument - .NET MongoDB: Deserialize BsonDocument from BsonType Array 无法从 BsonType“Array”反序列化“BsonDocument” - Cannot deserialize a 'BsonDocument' from BsonType 'Array' 无法反序列化“列表”<Object> &#39; 来自 BsonType &#39;文档&#39; - Cannot deserialize a 'List<Object>' from BsonType 'Document' 无法从BsonType&#39;Array&#39;反序列化&#39;BsonDocument - Cannot deserialize a 'BsonDocument from BsonType 'Array' 无法从 BsonType &#39;ObjectId&#39; 反序列化 &#39;String&#39; - Cannot deserialize a 'String' from BsonType 'ObjectId'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM