Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime comparison in LINQ not returning correct results

Tags:

c#

datetime

linq

I have the following LINQ to query the database and retreive deleted products from a particular date.

 return _myDbEntities.Log
            .Where(p => p.Action.Equals("Deleted") &&
            (p.ActionDate > fromDate))
            .Select(p => new DeletedProduct()
            {
                ProductId = p.ProductId,
                ActionDate = p.ActionDate
            }).ToList();

However, the query is retreiving values like product.ActionDate.Value = {12/8/2016 11:41:00 AM} when the fromDate was fromDate = {12/8/2016 11:41:00 AM}

The query clearly says GREATER THAN. What is happening here?

like image 838
Ali Kareem Raja Avatar asked Oct 18 '22 21:10

Ali Kareem Raja


1 Answers

There are fractions of a second to each of your properties. Most likely, your record wasn't created at an exact second, whereas any user-created time would be set as such.

Another possibility is the difference between datetime and datetime2 in SQL Server.

like image 64
krillgar Avatar answered Nov 15 '22 05:11

krillgar