c# 4.0 - Can't get EntityFunctions.TruncateTime() to work -
i using entity framework code first. using linq entity want grab record based on datetime value. here current code:
/// <summary> /// method check , see if parsed game exists on /// database. if yes, true returned, otherwise false returned. /// </summary> /// <param name="context">the db context use</param> /// <param name="hometeam">the name of home team game.</param> /// <param name="awayteam">the name of away team game.</param> /// <param name="date">the date of game</param> /// <returns></returns> public bool doesgamealreadyexist(pcontext context, string hometeam, string awayteam, string date) { string dtobjformat = "dd mmm yyyy"; datetime dt; datetime.tryparseexact(date, dtobjformat, cultureinfo.invariantculture, datetimestyles.none, out dt); var result = context.games.where(x => entityfunctions.truncatetime(x.start) == dt.date).firstordefault(); return result != null; }
the above code throws following error:
linq entities not recognize method 'system.nullable`1[system.datetime] truncatetime(system.nullable`1[system.datetime])' method, , method cannot translated store expression.
i tried following code, , same error:
var result = context.games.where(x => entityfunctions.truncatetime(x.start) == entityfunctions.truncatetime(dt.date)).firstordefault();
what doing wrong?
i faced problem when upgraded web application entity framework 5 entity framework 6. then, realized system.data.entity
dll needs removed application in order work entity framework 6. entity framework 6 not part of .net framework anymore , therefore independent of system.data.entity dll. in order truncate time, need use system.data.entity.dbfunctions.truncatetime(...)
method entityframework.dll. yes, solved problem.
bottom line : if using entity framework 6, first remove reference system.data.entity
dll , then, in code, replace entityfunctions.truncatetime(..)
system.data.entity.dbfunctions.truncatetime(...)
. [ entityframework.dll ]