Search code examples
c#entity-frameworklinqentity-framework-6linq-to-entities

Is there a way to get the long value from a Date in a Linq to Entity query?


First and foremost, yes, I know questions similar to this one have been asked ad nauseam. They typically need to compare two date times.

In my case, I am not trying to compare datetimes. I am trying to perform a mathematical operation which includes a Date property on the entity in the calculation.

I've been looking through the EntityFunctions, but unless I am mistaken, there doesn't seem to be a method that translates Dates to long/int values. Essentially the Ticks property, which Linq to Entities obviously doesn't support.

Is this possible?

For an easy example as to what I am trying to achieve:

var foos = db.Foos.OrderBy(f => f.Score / f.SomeDate.ToLong()).ToList();

If it helps, I'm using code first and the Dates I am referring to are represented by DateTime objects in C#.


Solution

  • you could try others functions to get an Int/Long value like

    EntityFunctions.DiffSeconds(fixedDate, f.SomeDate)
    

    Or

    EntityFunctions.DiffNanoseconds(fixedDate, f.SomeDate)