私はEntity Framework Code Firstを使用しています。 LINQ to Entityの使用DateTime値に基づいてレコードを取得したいと思います。ここに私の現在のコードは次のとおりです。EntityFunctions.TruncateTime()を動作させることができません
/// <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();
何私は間違っている?
非常に奇妙です。これはまるで別の静的クラス 'EntityFunctions'をどこかに持つように見えます。 「EntityFunctions」でF12を押した場合はどうなりますか?あなたは 'System.Data.Entity.dll'と' System.Data.Objects'名前空間に入っていますか? – Slauma
私は同じ問題を抱えています - alpha版のEF6を使用しています –