2

I'm trying to query a over mongoDB and I get the following error:

Unable to determine the serialization information for the expression: c.IndexMetadata.Indexed.HasValue.

where Indexed is a nullable datetime

my query is the following:

Collection.AsQueryable<Candidate>(c => !c.IndexMetadata.Indexed.HasValue || c.IndexMetadata.Updated.Value > c.IndexMetadata.Indexed.Value).ToList();

both indexed and updated are type of nullable datetime

I guess that's because there's no direct translation from HasValue into a mongo Query, any workaround?

4
  • You can't compare two fields with each other. You must compare them with constants unfortunately. A good way to figure out how to write a query is to write it in the shell first and make sure it works as you expect. Commented Apr 19, 2014 at 12:50
  • @CraigWilson I can refactor to avoid two columns comparison, but I still need to solve the hasValue error, thanks for the heads up! Commented Apr 19, 2014 at 16:58
  • 1
    Perhaps you can use != null instead for now. File a jira issue at jira.mongodb.com in the C# project. Commented Apr 19, 2014 at 17:13
  • @CraigWilson the null comparison did the trick thanks for the help! Commented Apr 19, 2014 at 17:39

1 Answer 1

7

You can compare the DateTime? to null instead of using HasValue:

Collection.AsQueryable<Candidate>(
    c => 
        c.IndexMetadata.Indexed == null || 
        c.IndexMetadata.Updated.Value > c.IndexMetadata.Indexed.Value).
    ToList();
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.