The following code fails with NullReference exception:
int? key = null; collection.Find(r => r.RequestId == requestId && (key == null || r.Key == key)).Limit(1).FirstOrDefaultAsync(); // Class, stored in collection public class Request { public string RequestId {get; set;} public int Key {get; set;} }
Stack trace attached.
Apparently, the problem is that the serializer selected based on the document property type, which is INT, rather than on the actual value type, which is INT?. Therefore, it later fails on NULL value, hence serializer expects ValueType, rather than reference type.
- is related to
-
CSHARP-1867 Non-nullable members cannot be compared to nullable values
- Closed