2013-01-17 54 views
0

你好誰能回答我爲什麼這個linq查詢不會按日期返回?Raven Db詢問日期

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

您的索引在第二個查詢中陳舊。評論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(); 
} 
+0

感謝您的回答,但它不會在第二次運行時陳舊,我得到相同的結果。但是,如果更改LINQ查詢通過ID查詢我得到一個結果。我正在尋找爲什麼日期不返回結果的答案? – Mantisimo

+0

我的不好。應該是!=在carExists檢查。示例已更新。運行它 - 它的工作。 –