8

我可能不明白 RavenDb 中的“In”扩展方法是如何工作的。我认为它相当于 SQL 世界中的 IN 命令,例如 WHERE Number IN (1, 2, 3)。但它的行为很奇怪。

// i have 3 records - two with Normal severity, one with Low severity
using (var session = store.OpenSession())
{
    session.Store(new TestObject { Name = "o1", Severity = Severity.Normal });
    session.Store(new TestObject { Name = "o2", Severity = Severity.Low });
    session.Store(new TestObject { Name = "o3", Severity = Severity.Normal });
    session.SaveChanges();
}

// this writes the Low severity record, it seems correct
using (var session = store.OpenSession())
{
    var data = session.Query<TestObject>()
        .Where(o => o.Severity.In(new[] { Severity.Low }))
        .OrderBy(o => o.Name).ToList();
    data.ForEach(r => Console.WriteLine(r));
}

// this writes also the Low severity record, it still seems correct
using (var session = store.OpenSession())
{
    var data = session.Query<TestObject>()
        .Where(o => o.Severity.In(new[] { Severity.Low, Severity.High }))
        .OrderBy(o => o.Name).ToList();
    data.ForEach(r => Console.WriteLine(r));
}

// this writes all records, still seems correct
using (var session = store.OpenSession())
{
    var data = session.Query<TestObject>()
        .Where(o => o.Severity.In(
            new[] { Severity.High, Severity.Low, Severity.Normal }))
        .OrderBy(o => o.Name).ToList();
    data.ForEach(r => Console.WriteLine(r));
}

// but this does not write anything 
// despite there are 2 records with Normal severity
using (var session = store.OpenSession())
{
    var data = session.Query<TestObject>()
        .Where(o => o.Severity.In(new[] { Severity.High, Severity.Normal }))
        .OrderBy(o => o.Name).ToList();
    data.ForEach(r => Console.WriteLine(r));
}

// and this does not write anything either, 
// I just tried whether the order of values in the array matters
using (var session = store.OpenSession())
{
    var data = session.Query<TestObject>()
        .Where(o => o.Severity.In(new[] { Severity.Normal, Severity.High }))
        .OrderBy(o => o.Name).ToList();
    data.ForEach(r => Console.WriteLine(r));
}

还是我在 RavenDb/Lucene 引擎中发现了一个错误?

编辑

我发现了另一个奇怪的事情。它必须与枚举成员的字母顺序有关。当我将 Severity.Normal 重命名为 Severity.A 时,最后两个查询运行正常并返回结果。当我将 Severity.Normal 重命名为 Severity.La 时,它仍然有效(因为 La < Low)。但是当我将 Severity.Normal 重命名为 Severity.Lu (Lu > Low) 时,它会中断并且最后两个查询不再返回结果。原始样本不起作用,因为正常 > 低。但我仍然想知道为什么会发生这种情况,因为这对我来说毫无意义。

4

1 回答 1

6

您遇到了过时的索引,这就是为什么您会得到奇怪的结果。您需要先等待索引完成。

于 2012-11-24T22:57:34.790 回答