Raven Db查询日期

时间:2013-01-17 17:09:38

标签: ravendb

大家能否回答我为什么这个linq查询不会按日期返回?

DocumentStore store = new DocumentStore { Url = "http://localhost:8080/" };
store.Initialize();

using (var session = store.OpenSession())
{
    bool carExists = session.Query<Car>().Any(c => c.CarId == 1);

    if (!carExists)
    {
        Car myNewCar = new Car
            {
                CarId = 1,
                ManufactureDate = new DateTime(2010, 1, 1),
                Name = "Porshe"
            };

        session.Store(myNewCar);
        session.SaveChanges();
    }

    IQueryable<Car> cars = session.Query<Car>()
            .Where(c => c.ManufactureDate == new DateTime(2010, 1, 1));

    foreach (var car in cars)
    {
        Console.WriteLine(car.Name);
    }

    Console.ReadKey();
}

1 个答案:

答案 0 :(得分:1)

您的索引在第二个查询中是陈旧的。查看the documentation

此外,您的第一个查询应该是Load。查看the documentation

DocumentStore store = new DocumentStore { Url = "http://localhost:8080/" };
store.Initialize();

using (var session = store.OpenSession())
{
    bool carExists = session.Load<Car>(1) != null;

    if (!carExists)
    {
        Car myNewCar = new Car
            {
                CarId = 1,
                ManufactureDate = new DateTime(2010, 1, 1),
                Name = "Porshe"
            };

        session.Store(myNewCar);
        session.SaveChanges();
    }

    IQueryable<Car> cars = session.Query<Car>()
            .Customize(x=> x.WaitForNonStaleResults()) // only for testing!
            .Where(c => c.ManufactureDate == new DateTime(2010, 1, 1));

    foreach (var car in cars)
    {
        Console.WriteLine(car.Name);
    }

    Console.ReadKey();
}
相关问题