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