1

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

1
  • people.Where(x => (x.CreateDate - dtMatch).TotalHours > 0) usually works. Can you tell us what's the difference between those two statements you saw? Commented Feb 6, 2017 at 14:26

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Using Hours gets only the Hours component which is always less than 24. That's a bad idea!
Good answer btw
1

Perhaps, you should consider the case if dtMatch is over CreateDate and the case if it is under:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.