【问题标题】:Difficulty creating proper RavenDB query难以创建正确的 RavenDB 查询
【发布时间】:2015-08-09 00:14:45
【问题描述】:

我正在尝试在 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;
}

【问题讨论】:

    标签: ravendb


    【解决方案1】:

    诀窍在于 ravendb linq 提供程序没有在您的列表上运行,因此 score.Any() 对它的意义为零——它可以编译,但您可以看到它在运行时死亡。

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

    【讨论】:

      【解决方案2】:

      如果我们修改了您的示例以使用 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>();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-08
        • 2021-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-10
        • 1970-01-01
        相关资源
        最近更新 更多