Ravendb Raven数据库查询日期

Ravendb Raven数据库查询日期,ravendb,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); i

嗨,有人能回答我为什么这个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();
}
DocumentStore=newdocumentstore{Url=”http://localhost:8080/" };
store.Initialize();
使用(var session=store.OpenSession())
{
bool carExists=session.Query().Any(c=>c.CarId==1);
如果(!carExists)
{
我的新车
{
CarId=1,
ManufactureDate=新日期时间(2010,1,1),
Name=“Porshe”
};
session.Store(我的新车);
session.SaveChanges();
}
IQueryable cars=session.Query()
其中(c=>c.ManufactureDate==新日期时间(2010,1,1));
foreach(车辆中的var车辆)
{
控制台写入线(车名);
}
Console.ReadKey();
}

您的索引在第二次查询时已过时。复习

此外,您的第一个查询应该是加载。复习

DocumentStore=newdocumentstore{Url=”http://localhost:8080/" };
store.Initialize();
使用(var session=store.OpenSession())
{
bool carExists=session.Load(1)!=null;
如果(!carExists)
{
我的新车
{
CarId=1,
ManufactureDate=新日期时间(2010,1,1),
Name=“Porshe”
};
session.Store(我的新车);
session.SaveChanges();
}
IQueryable cars=session.Query()
.Customize(x=>x.WaitForNonSaleResults())//仅用于测试!
其中(c=>c.ManufactureDate==新日期时间(2010,1,1));
foreach(车辆中的var车辆)
{
控制台写入线(车名);
}
Console.ReadKey();
}

谢谢您的回答,但第二次运行时不会过时,我会得到相同的结果。但是,如果将linq查询更改为按id查询,我将得到一个结果。我在寻找一个答案,为什么日期没有返回结果?我的错。应该是在carExists支票上。示例已更新。运行它-它工作。
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();
}