Tuan
Tuan

Reputation: 952

MongoDB C# Linq driver with nullable types

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?

Upvotes: 3

Views: 2326

Answers (3)

Ted Elliott
Ted Elliott

Reputation: 3503

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)

Upvotes: 1

Tuan
Tuan

Reputation: 952

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

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

Upvotes: 0

Mzf
Mzf

Reputation: 5260

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

Upvotes: 0

Related Questions