Sto utilizzando il codice di Entity Framework First. Usando LINQ su Entity voglio prendere un record basato su un valore DateTime. Qui è il mio codice corrente:Impossibile ottenere EntityFunctions.TruncateTime() per funzionare
/// <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;
}
Il codice sopra tiri il seguente errore:
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.
Ho anche provato il seguente codice ed ottengo lo stesso errore:
var result = context.Games.Where(x => EntityFunctions.TruncateTime(x.Start) == EntityFunctions.TruncateTime(dt.Date)).FirstOrDefault();
Cosa sto sbagliando?
Molto strano. Sembra quasi che tu abbia un altro 'EntityFunctions 'di classe statica. Cosa succede se si preme F12 su "EntityFunctions"? Atterri nello spazio dei nomi 'System.Data.Entity.dll' e' System.Data.Objects'? – Slauma
Ho lo stesso problema - utilizzando la versione alpha di EF6 –