3

I am using mongodb c# driver and tried the following query

collection.AsQueryable().Where(x => x.IsArchived.GetValueOrDefault())

where IsArchived is of type bool? (nullable).

I get the following runtime error:

Unsupported where clause: x.IsArchived.GetValueOrDefault().

Does anybody know how I can query nullable types?

3 Answers 3

1

I know we use nullable types in our domain, but can't seem to find any specific instances of quering them. You might try this:

collection.AsQueryable().Where(x => x.IsArchived == true)

or this if that doesn't compile:

collection.AsQueryable().Where(x => x.IsArchived == (bool?) true)
Sign up to request clarification or add additional context in comments.

Comments

0

Try

collection.AsQueryable().Where(x => x.IsArchived!= null && x.IsArchived)

your expression is translate to mongo query , that is not supprt the C# GetValueOrDefault which give you the exception

1 Comment

compiler complains "&& x.IsArchived" because this is not valid. using "&& x.IsArchived.Value" causes same runtime error.
0

i found out a workaround, though it is not very nice:

collection.AsQueryable().Where(x => x.IsArchived ?? false)

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.