难以创建正确的RavenDB查询

时间:2015-07-21 18:57:00

标签: ravendb

我正在尝试在RavenDB查询中实现下面的逻辑,但是接收

System.NotSupportedException: Could not understand expression

scores.Any表达式相关。我理解为什么会这样,但是我很难想出一个工作选项。

public IRavenQueryable<Person> Apply(IRavenQueryable<Person> query)
{
    var scores = new List<string>();
    if (_a) scores.Add("A");
    if (_b) scores.Add("B");
    if (_c) scores.Add("C");
    if (_u)
    {
        scores.Add("");
        scores.Add(" ");
        scores.Add("\t");
        scores.Add(null);
    }

    return from p in query
           where scores.Any(score => score == p.Score)
           select p;
}

2 个答案:

答案 0 :(得分:1)

诀窍在于ravendb linq提供者不会在你的列表上运行所以得分.Any()对它没有任何意义 - 它会编译但是你可以看到它在运行时死掉。

诀窍是稍微反转一下,如果我没记错的话,询问p.Score是否在分数数组中。

答案 1 :(得分:0)

如果我们修改您的示例以使用IDocumentQuery,它应该与:

一起使用
public IDocumentQuery<Person> Apply(IDocumentQuery<Person> query)
{
    var scores = new List<string>();
    if (_a) scores.Add("A");
    if (_b) scores.Add("B");
    if (_c) scores.Add("C");
    if (_u)
    {
        scores.Add("");
        scores.Add(" ");
        scores.Add("\t");
        scores.Add(null);
    }

    return query.AndAlso().WhereIn(x => x.Score, scores);
}

您的初始文档查询可能类似于:

var myQuery = RavenSession.Advanced.DocumentQuery<Person>();