"Nullable object must have a value" when Where uses a method?

urig

I'm trying to run a LINQ query against an Azure DocumentDB collection. When I run my query I keep getting an AggregateException that contains an InvalidOperationException with the message:

Nullable object must have a value

I've reduced this issue to the following (somewhat contrived) example:

When I run this code, I get the above mentioned exception thrown from the call to ToArray()

public class MyDocument { ... }

public void RunQuery()
{
    var query = documentDbClient
        .CreateDocumentQuery<MyDocument>()
        .Where(doc => GetDoc(doc) != null);
    var results = query.ToArray()
}

public MyDocument GetDoc(MyDocument myDocument)
{
    return myDocument;
}

In contrast, when I run the code below no exception is thrown and I get back good results from the DocumentDB collection.

public void RunQuery()
{
    var query = documentDbClient
        .CreateDocumentQuery<MyDocument>()
        .Where(doc => doc != null);
    var results = query.ToArray()
}

Why the difference in behavior between the two code samples?

Notes:
- While GetDoc() is a stand in for my more complex predicate logic, the code above reproduces the issue exactly. I'm not withholding any shenanigans inside GetDoc() or other methods :)
- The issue occurs even when GetDoc() is made static.
- Just tried to reproduce with a List<MyDocument> instead of documentDbClient and no exception was thrown. Indicates something in the underlying data provider = Azure DocumentDB's IDatabaseClient.

urig

With help from @andrew-liu and @will (thanks!) I've been able to figure out that:

At the time of the call to Where() in my above example, the predicate inside is interpreted (rather than executed) as a LINQ expression. At the time of the call to ToArray() the LINQ expression is executed against the DocumentDB .NET SDK's LINQ provider.

Because the SDK is not aware of my GetDoc() method, it throws the exception with the cryptic message "Nullable object must have a value".

In v1.9 of the DocumentDB SDK for .Net the error message is much clearer. A DocumentQueryException is thrown with a message like "Method "GetDoc()" is not supported".

You can get similar feedback from v1.8 of the SDK by invoking ToString() on the query like so: query.ToString().

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

best method to solve Nullable object must have a value in datetime

From Dev

ASP.NET MVC 5 Razor throws 'Nullable object must have a value' even when the object has value

From Dev

Linq: Nullable Object must have Value

From Dev

Nullable object must have a value datetime

From Dev

Decimal? - Nullable object must have a value

From Dev

Anonymous Type "Nullable object must have a value" Error

From Dev

Nullable object must have a value while using datepicker control with dropdown control

From Dev

"Nullable object must have a value" exception after checking for null on a non-primitive/non-struct object

From Dev

When and Where to call EventQueue.invokeLater() method

From Dev

When and where to use FindElement method of By class

From Dev

When and where to use FindElement method of By class

From Dev

What is the method signature when it uses an external param name in Swift

From Dev

Angular method executes indefinitely when it uses with the ng-style

From Java

Transform this into method that uses streams?

From Dev

Uses of method BinarySearch with ArrayList

From Dev

Query uses Index Scan instead of Seek when OR @param = 1 added to WHERE clause

From Dev

Zend Framework 1 cannot use where() method when using union()

From Dev

When can a service be destroyed and where inserting the stopSelf() method?

From Dev

When a method uses a cache, but occasionally does I/O, should it return T, or Task<T>?

From Dev

Mock.Verify uses current value of list, not the value of the list when the method being verified was called

From Dev

How do I run a JUnit Test on the exception handlers of a method and when a function normally uses a file for input?

From Dev

EmberJS "set" method on object doesn't seems to work when uses filter with Ember Data

From Dev

How to write unit test with mocks when method uses ObjectMapper.writeValueAsString

From Dev

How to avoid deadlock when the called method uses the same lock that the caller already locked?

From Dev

Where is the default scalaVersion sbt uses?

From Dev

A where clause that uses nested attributes?

From Dev

A where clause that uses nested attributes?

From Dev

Where does the method 'where' come from when using the Omniauth-Facebook gem?

From Dev

Multiple uses of replaceAll() String method

Related Related

  1. 1

    best method to solve Nullable object must have a value in datetime

  2. 2

    ASP.NET MVC 5 Razor throws 'Nullable object must have a value' even when the object has value

  3. 3

    Linq: Nullable Object must have Value

  4. 4

    Nullable object must have a value datetime

  5. 5

    Decimal? - Nullable object must have a value

  6. 6

    Anonymous Type "Nullable object must have a value" Error

  7. 7

    Nullable object must have a value while using datepicker control with dropdown control

  8. 8

    "Nullable object must have a value" exception after checking for null on a non-primitive/non-struct object

  9. 9

    When and Where to call EventQueue.invokeLater() method

  10. 10

    When and where to use FindElement method of By class

  11. 11

    When and where to use FindElement method of By class

  12. 12

    What is the method signature when it uses an external param name in Swift

  13. 13

    Angular method executes indefinitely when it uses with the ng-style

  14. 14

    Transform this into method that uses streams?

  15. 15

    Uses of method BinarySearch with ArrayList

  16. 16

    Query uses Index Scan instead of Seek when OR @param = 1 added to WHERE clause

  17. 17

    Zend Framework 1 cannot use where() method when using union()

  18. 18

    When can a service be destroyed and where inserting the stopSelf() method?

  19. 19

    When a method uses a cache, but occasionally does I/O, should it return T, or Task<T>?

  20. 20

    Mock.Verify uses current value of list, not the value of the list when the method being verified was called

  21. 21

    How do I run a JUnit Test on the exception handlers of a method and when a function normally uses a file for input?

  22. 22

    EmberJS "set" method on object doesn't seems to work when uses filter with Ember Data

  23. 23

    How to write unit test with mocks when method uses ObjectMapper.writeValueAsString

  24. 24

    How to avoid deadlock when the called method uses the same lock that the caller already locked?

  25. 25

    Where is the default scalaVersion sbt uses?

  26. 26

    A where clause that uses nested attributes?

  27. 27

    A where clause that uses nested attributes?

  28. 28

    Where does the method 'where' come from when using the Omniauth-Facebook gem?

  29. 29

    Multiple uses of replaceAll() String method

HotTag

Archive