Search code examples
c#simple.data

simple.data orm, how to search by date?


I am using simple.data for my project. I have a column createdDate. this is a datetime column. if i passed in a date string "05/06/2006". I need to get all records created in that day. how can i do that using simple.data?

following code is not working. i only need to compare the date, not the time. hwo can i modify it to get it work.

        var list = _db.DocumentLog.All();

        if (!string.IsNullOrWhiteSpace(searchDate))
        {
            var dt = DateTime.ParseExact(searchDate, "MM/dd/yyyy", null);

            list = list.Where(_db.DocumentLog.CreatedDate == dt);
        }

Solution

  • Replace your search with:

    list.Where(_db.DocumentLog.CreatedDate >= dt 
            && _db.DocumentLog.CreatedDate < dt.AddDays(1));
    

    That will give you everything created on or after midnight of the given date, but before the next day - i.e. one full day.