I find myself using the ‘pattern’ below rather unsettlingly often, when I want to select entities with based only on the date part of a DateTime property. EF doesn’t parse the DateTime.Date property to T-SQL, so I end up using this code:
var nextDay = raceDate.Date.AddDays(1);
return EntityContext.RacingInfoes.SingleOrDefault(ri => ri.RaceDate >= raceDate && ri.RaceDate < nextDay);
It’s the most readable solution I have found so far, but I don’t like repeating it everywhere. However, I can’t encapsulate it in any method as that method is not recognised by the Linq to Entities parser. Is there anything I can do about this?
You can encapsulate it by writing a method like this:
Then you’d write:
(
OnDateis a bad name, but you see what I mean…)