如何使用 RavenDb 构建正确的 WHERE 子句 [英] How to construct a proper WHERE clause with RavenDb

查看:29
本文介绍了如何使用 RavenDb 构建正确的 WHERE 子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了两种调用形式,这个

I have experimented with 2 forms of the call, this one

 products = DocumentSession.Query<Product>()
      .Statistics(out stats)
      .Where(p => p.INFO2.StartsWith(term1))
      .Where(p => p.INFO2.StartsWith(term2))
      .Where(p => p.INFO2.StartsWith(term3)) 
      .OrderByField(columnToSortBy, columnToSortByAsc)
      .Skip(pageIndex * pageSize)
      .Take(pageSize)
      .ToList()
      ;

这样

  products = DocumentSession.Query<Product>()
      .Statistics(out stats)
      .Where(p => p.INFO2.StartsWith(term1) & p.INFO2.StartsWith(term2) & p.INFO2.StartsWith(term3))
      .OrderByField(columnToSortBy, columnToSortByAsc)
      .Skip(pageIndex * pageSize)
      .Take(pageSize)
      .ToList()
      ;

第一个返回更符合我预期的记录,而第二个似乎返回 Product 类型的所有文档.从 LINQ 表达式的角度来看,这 2 个之间有什么区别,我是否忽略了任何可能否定我正在尝试完成的任务,即 3 个词条查询,每个词条都被 AND 运算在一起.

The first one returns records that are more in-line with my expectations, while the seconds seems to return ALL documents of type Product. What are the differences between the 2 from a LINQ expression point of view, and have I overlooked anything that might negate what I am trying to accomplish, which is a 3 term query and each term being AND'd together.

根据 Russ 修改代码.

revised code per Russ.

  string t1 = terms[0];
  string t2 = terms[1];
  string t3 = terms[2];

  products = DocumentSession.Query<Product>()
      .Statistics(out stats)
      .Where(p => p.INFO2.StartsWith(t1) && p.INFO2.StartsWith(t2) && p.INFO2.StartsWith(t3)) 
      .OrderByField(columnToSortBy, columnToSortByAsc)
      .Skip(pageIndex * pageSize)
      .Take(pageSize)
      .ToList()
      ;

编辑 2:这是您将脸朝下砸在键盘或任何其他实体对象上的地方...必须使用标准的 C# And and Or 回到这里的基本原理

EDIT 2: This is where you smash your face down on the keyboard, or any other solid object for that matter... Gotta get back to the basic here with standard C# And and Or

谢谢,斯蒂芬

推荐答案

在第二个区块中,你正在做一个 &而不是 &&因此,它不是进行 AND 比较,而是尝试进行按位运算.

In the second block you're doing an & instead of an && so instead of being an AND comparison, it's trying to do a bitwise operation.

在第二种情况下,您可以更改:

in the 2nd case, you can change this:

.Where(p => p.INFO2.StartsWith(terms[0]) & p.INFO2.StartsWith(terms[1]) & p.INFO2.StartsWith(terms[2]))

为此:

.Where(p => p.INFO2.StartsWith(terms[0]) && p.INFO2.StartsWith(terms[1]) && p.INFO2.StartsWith(terms[2]))

这使它成为正确的 AND 子句.

Which makes it proper AND clause.

第二次如果这是一个 AND 运算,那么您不需要 3 个术语 - 您需要一个术语,否则您将与同一字符串的 3 个实例进行比较.

2nd edit: If this is meant to be an AND operation, then you don't need 3 terms - you need a single term, otherwise you'll be comparing against 3 instances of the same string.

terms[0] = "test";
terms[1] = "test";
terms[2] = "test";

.Where(p => p.INFO2.StartsWith(terms[0]) && p.INFO2.StartsWith(terms[1]) && p.INFO2.StartsWith(terms[2]))

string term = "test";

.Where(p => p.INFO2.StartsWith(term) && p.INFO2.StartsWith(term) && p.INFO2.StartsWith(term))

仅提及这一点,因为这可能会使您的代码在未来更难维护.

Just mentioning this as this may make your code harder to maintain in the future.

这篇关于如何使用 RavenDb 构建正确的 WHERE 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆