条件查询中的Json日期格式,用于检查当前日期

时间:2011-12-15 19:17:11

标签: json linq datetime indexing ravendb

如何在此处转换日期时间。来自数据库(RDate)的日期时间是Json格式“/ Date(1323975591369)/”,我正在与DateTime.Today进行比较

 int count = session.Query<Movies>("Movie/CurrentList")   
                    .Where(x => x.RDate== DateTime.Today)
                    .Count(); 

现在即使有符合条件的记录,我在计数中显示零结果。

1 个答案:

答案 0 :(得分:1)

ZVenue,您可能只想查询 RDate 属性的Date组件。如果是这种情况,只需正确扩展您的查询。

但是,我不确定你是否掌握了基础知识,所以我已经整理了一个小样本应用程序。事实上很无聊,但我希望它有所帮助:

class Movie
{
    public string Id { get; set; }
    public string Name { get; set; }
    public DateTime Date { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        using (var documentStore = new DocumentStore { Url = "http://localhost:8080/" })
        {
            documentStore.Initialize();

            //SeedData(documentStore);

            using (var documentSession = documentStore.OpenSession())
            {
                var movies = documentSession.Query<Movie>()
                    .Where(x => x.Date.Date == new DateTime(2011, 6, 2))
                    .ToList();

                foreach (var movie in movies)
                {
                    Console.WriteLine(string.Format("Name: {0}", movie.Name));
                }
            }
        }

        Console.Read();
    }

    private static void SeedData(IDocumentStore documentStore)
    {
        var movies = new Movie[]
                         {
                             new Movie {Name = "May", Date = new DateTime(2011, 5, 1)},
                             new Movie {Name = "June", Date = new DateTime(2011, 6, 1)},
                             new Movie {Name = "June2a", Date = new DateTime(2011, 6, 2)},
                             new Movie {Name = "June2b", Date = new DateTime(2011, 6, 2)},
                             new Movie {Name = "June2c", Date = new DateTime(2011, 6, 2, 5, 0, 0)},
                             new Movie {Name = "June2d", Date = new DateTime(2011, 6, 2, 6, 0, 0)},
                             new Movie {Name = "June3", Date = new DateTime(2011, 6, 3)},
                             new Movie {Name = "July", Date = new DateTime(2011, 7, 1)},
                         };

        using (var documentSession = documentStore.OpenSession())
        {
            foreach (var movie in movies)
            {
                documentSession.Store(movie);
            }

            documentSession.SaveChanges();
        }
    }
}