Toshi Toshi - 4 years ago 136
C# Question

Using EntityFunctions without entity framework

I have a Linq query:

people.Where(x => EntityFunctions.DiffHours(x.CreateDate, dtMatch) > 0)

that does only work within
Linq 2 Entities

How do I make this method run without Entities?

What I've tried so far:

people.Where(x => (x.CreateDate - dtMatch).TotalHours > 0)

... seems to give a different result

Answer Source

In short: To make the 2 statements equivalent change TotalHours to Math.Floor((x.CreateDate - dtMatch).TotalHours)

people.Where(x => Math.Floor((x.CreateDate - dtMatch).TotalHours) > 0) 

The TotalHours will return a decimal. So if there was 30 minutes between the 2 dates then 0.5 would be returned. As .5 is greater than 0 the above returns true. If you want whole hours change it to Math.Floor(TotalHours) instead of TotalHours. This will round it down to the next largest integer.

EntityFunctions.DiffHours - This returns an integer representing the whole number of hours, so if it was 30 min. difference then 0 would be the result.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download