0

I'm making a call to Azure Cosmos DB where I know the data I'm querying does NOT exist.

I was expecting to get a null value but instead I'm getting:

Enumeration yielded no results

enter image description here

How do I test whether I received a value or not? I was testing for null which doesn't work because the outcome is not null.

My code looks something like this:

var result = await _client.ReadQuery<myObject>(AccountsCollection, sql, pa);
if(result == null)
   return null;
5
  • 1
    Check for .Any() in addition to null: if(result == null || !result.Any()) Commented Oct 4, 2017 at 21:10
  • Still skipping the line where I check for null or Any(). I think it has something to do with the async call. I guess I'm getting a promise not a result. Commented Oct 4, 2017 at 21:13
  • Well 2 things, use var only when the type is obvious (i.e var foo = new DateTime()). Also, make sure your any logic matches what I put in the comment above. If result is null OR result does NOT have any Commented Oct 4, 2017 at 21:15
  • Also, it looks like you are using HttpClient to read from Cosmos instead of the Cosmos library. Any reason for that? Commented Oct 4, 2017 at 21:16
  • 1
    Sorry, it does work! I didn't use the "not" i.e. ! before Any(). I am using Cosmos DB library. I'm not using HttpClient. I placed my Cosmos DB logic in a different class which I called _client which is only a wrapper around Cosmos DB library. Thank you for your help! Commented Oct 4, 2017 at 21:19

1 Answer 1

1

Instead of just checking on result == null you should use the LINQ extension method .Any() to see if there are any items that match a condition (in your case the condition is just anything existing in the collection):

if(result == null || !result.Any())
{
    return null;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.