5

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?

12
  • Check the fraction of a second (or display it as timestamp). Maybe the difference is there Dec 8, 2016 at 12:18
  • 2
    What type is ActionDate? Could you try (p.ActionDate.Value > fromDate)? Dec 8, 2016 at 12:19
  • In the query you are using product.ActionDate but tit seems you need product.ActionDate.Value
    – Guy
    Dec 8, 2016 at 12:19
  • try to see the result SQL query, in debug mode
    – Vladimir
    Dec 8, 2016 at 12:20
  • Datetime also stores the ms, have you checked that difference? Also have you analysed the produced query?
    – Silvermind
    Dec 8, 2016 at 12:22

2 Answers 2

3

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.

0
0

The DateTime type stores time at much higher precision than seconds. They could be differing at millisecond or even tick (100 nanoseconds) level.

If you want to compare on a higher level, try this:

(p.ActionDate.Ticks / 10000000) > (fromDate.Ticks / 10000000)

Where 10000000 is the number of ticks in a second. Since the /is an integer division that does truncate the fraction, you turn ticks into full seconds.

UPDATE:

It seems like you are using entity framework. The comparison above will possibly not work there. The solution is to run your original query against the database, do a ToList and then filter the results again in a LINQ2Objects query using the logic above.

6
  • You do not want to pull in a list of everything to do additional filtering on it in memory. Depending on the size of the project, this could generate a LOT of extra data being returned from the database that then needs to be parsed into C# objects. Utilize the tools that you have (SQL Server), and let that do the filtering for you.
    – krillgar
    Dec 8, 2016 at 12:46
  • @krillgar: You don't pull a list of everything. You pull the original query result, which already does the comparison. The second filter is to filter the few results where the values are differing only on sub-second level.
    – Sefe
    Dec 8, 2016 at 12:49
  • OK, I misunderstood. If the OP doesn't have the ability to change the database to a datetime2 column, this would work. However, if they can make that change, you wouldn't need to do this.
    – krillgar
    Dec 8, 2016 at 12:52
  • @krillgar: Correct, if they can make the change. That is not always possible or the DB could change later. If you run a second filter query on a general basis, you are safe in any possible scenario and you lose nothing.
    – Sefe
    Dec 8, 2016 at 12:55
  • In most cases, yeah. I work in an enterprise environment, and just spent most of the year working on a request system. I've been in the mindset of several thousand requests that were huge objects being created every day, so pulling several hundred of those in for every query would have created a big lag.
    – krillgar
    Dec 8, 2016 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.