I have data in my MongoDB where I am storing a nullable int, most often as null. My model on the C# side has the variable as int? MaterialID
.
When I run .Query.FirstOrDefault()
, I get an error "Input string was not in a correct format." whenever it tries to read the null value from Mongo (ints work fine).
I managed to get around this by changing my int? to a string and then later casting that back to an int?, but that's a horrible hack that I really don't like. Anyone have any ideas as to how I can read null from Mongo into my int? on the C# side?
Thanks.
My 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"
}
]