Używam Entity Framework Code First. Używanie LINQ do Entity Chcę pobrać rekord na podstawie wartości DateTime. Tu jest mój bieżący kod:Nie można uzyskać EntityFunctions.TruncateTime() do pracy
/// <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;
}
Powyższy kod wyrzuca następujący błąd:
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.
Próbowałem też poniższy kod i uzyskać ten sam błąd:
var result = context.Games.Where(x => EntityFunctions.TruncateTime(x.Start) == EntityFunctions.TruncateTime(dt.Date)).FirstOrDefault();
Co czy robię źle?
Bardzo dziwne. Wygląda to niemal tak, jakbyś miał gdzieś inną klasę statyczną "EntityFunctions". Co jeśli trafisz F12 na "EntityFunctions"? Czy lądujesz w przestrzeni nazw 'System.Data.Entity.dll' i' System.Data.Objects'? – Slauma
Mam ten sam problem - używanie wersji alfa EF6 –