简体   繁体   中英

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

Everything seems to be going well, except I'm not sure how to handle a MongoDB Null value

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

       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."

Can anyone suggest a reference or different approach to handle the nulls? Will BsonString.Empty help me here?

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

Allowed me to pass nulls into those fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM