Raven数据库按问题排序

时间:2013-04-05 15:15:53

标签: c# linq sql-order-by ravendb

我收集了文件:

{
"Name": "MyName",
"LabelKey": "MyLabelKey"
}

在集合中,LabelKey可以为null或不在文档中。如果我尝试使用linq命令此列表:

session.Query<MyMetaData>().OrderBy(x => x.LabelKey).ToList()

我只获得LabelKey不为空的记录。因此,如果我有100个文档的集合,并且只有2个具有LabelKey值,在我订购后我将只得到2.我需要订购所有集合,即使他们有LabelKey null。如果我尝试检查非null,我会得到异常:

Could not understand how to translate '(x.LabelKey != null)' to a RavenDB query.
Are you trying to do computation during the query?
RavenDB doesn't allow computation during the query, computation is only allowed during index. Consider moving the operation to an index.

任何想法我如何订购所有收藏品?

1 个答案:

答案 0 :(得分:1)

方法1

不存储空值。在路上,检查null并替换为空字符串。

方法2

创建静态索引。在Map表达式中,使用null合并运算符,例如:

LabelKey = x.LabelKey ?? string.Empty

然后使用该索引进行查询。