Mantisimo
Mantisimo

Reputation: 4283

Raven Db Querying dates

Hi can anyone answer me why this linq query wont return by date?

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();
}

Upvotes: 0

Views: 566

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241573

Your index is stale at the second query. Review the documentation.

Also, your first query should be a Load instead. Review 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();
}

Upvotes: 2

Related Questions