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?

4

3 回答 3

1

我找到了一个解决方法,虽然它不是很好:

collection.AsQueryable().Where(x => x.IsArchived ?? false)
于 2013-06-14T11:34:19.450 回答
1

我知道我们在我们的域中使用可为空的类型,但似乎找不到任何特定的查询它们的实例。你可以试试这个:

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

或者这个如果不编译:

collection.AsQueryable().Where(x => x.IsArchived == (bool?) true)
于 2013-06-27T16:34:09.010 回答
0

尝试

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

您的表达式被转换为 mongo 查询,这不支持GetValueOrDefault给您异常的 C#

于 2013-06-13T17:09:21.057 回答