我首先使用实体框架代码。使用 LINQ to Entity 我想获取基于 DateTime 值的记录。这是我当前的代码:
/// <summary>
/// A method to check and see if the Parsed Game already exists on the
/// database. If Yes, then True is returned, otherwise False is returned.
/// </summary>
/// <param name="context">The Db Context to use</param>
/// <param name="homeTeam">The Name of the Home Team for the Game.</param>
/// <param name="awayTeam">The Name of the Away Team for the Game.</param>
/// <param name="date">The Date of the 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;
}
上面的代码抛出以下错误:
LINQ to Entities does not recognize the method 'System.Nullable`1[System.DateTime] TruncateTime(System.Nullable`1[System.DateTime])' method, and this method cannot be translated into a store expression.
我也尝试了下面的代码,我得到了同样的错误:
var result = context.Games.Where(x => EntityFunctions.TruncateTime(x.Start) == EntityFunctions.TruncateTime(dt.Date)).FirstOrDefault();
我究竟做错了什么?