【发布时间】:2017-09-01 08:37:02
【问题描述】:
我想使用 RavenDb 4.0.0-beta-40018 查询包含动态属性的实体,但不知道该怎么做。
class Foo {
public int Id { get; set; }
public DateTime CreatedAt { get; set; }
public string Name { get; set; }
public dynamic Attributes { get; set; }
}
{
"Attributes": {
"IsFeatured": true
},
"CreatedAt": "2017-08-30T15:53:21.1881668Z",
"Name": "Featured Foo"
}
这是我尝试使用的查询。
const string propertyName = "IsFeatured";
var results = session.Query<Foo>()
.Where(x => x.Attributes != null)
.Where(x => x.Attributes[propertyName] != null)
.Where(x => x.Attributes[propertyName] == true);
很遗憾,我什至无法编译这段代码,因为我遇到了编译错误 (Error: An expression tree may not contain a dynamic operation)。
我不认为这是在动态属性中搜索(使用 ravendb)的好方法。有更好的方法吗?
【问题讨论】:
-
其实是个好问题。我相信在 RavenDB 3.5 之前你可以使用
var results = DocumentSession.Advanced.LuceneQuery<Foo>() .Where("Attributes.IsFeatured: true") .ToList();,但由于 LuceneQuery 已被 RQL 弃用,我不知道现在是如何做到的。 -
仅供参考:Raven 查询语言 (RQL) 不在 Raven 3.5 中。它将在 Raven 4.0 中。