framework c#-4.0 entity-framework-4 linq-to-entities entity-functions

c#-4.0 - call function entity framework



No se puede hacer que EntityFunctions.TruncateTime() funcione (3)

Estoy usando el código de Entity Framework primero. Usando LINQ para Entidad, quiero tomar un registro basado en un valor de DateTime. Aquí está mi código actual:

/// <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; }

El código anterior arroja el siguiente error:

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.

También probé el siguiente código y obtengo el mismo error:

var result = context.Games.Where(x => EntityFunctions.TruncateTime(x.Start) == EntityFunctions.TruncateTime(dt.Date)).FirstOrDefault();

¿Qué estoy haciendo mal?


No DbFunctions.TruncateTime la referencia System.Data.Entity , simplemente cambié la llamada de DbFunctions.TruncateTime a System.Data.Entity.DbFunctions.TruncateTime y funcionó para mí.

Estoy usando la Entidad 6.


Recientemente enfrenté este problema cuando actualicé mi aplicación web de Entity Framework 5 a Entity Framework 6. Luego, me di cuenta de que System.Data.Entity DLL debe eliminarse de la aplicación completamente para poder trabajar con Entity Framework 6. Entity Framework 6 ya no es parte de .NET Framework y, por lo tanto, es independiente de System.Data.Entity dll. Para truncar el tiempo, debe usar el método System.Data.Entity.DbFunctions.TruncateTime(...) de EntityFramework.dll. Sí, eso resolvió mi problema.

Conclusión : si está utilizando Entity Framework 6, primero QUITE la referencia System.Data.Entity DLL y luego, en su código, reemplace EntityFunctions.TruncateTime(..) con System.Data.Entity.DbFunctions.TruncateTime(...) . [De EntityFramework.dll]


DateTime? dt = DateTime.Parse(sText); campaigns = _CampaignMasterRepository.Get().OrderByDescending(a => a.LaunchOn).Where(a => a.UserId == obj_User.ID && a.CampaignStatus == (int)OmevoEnums.CampaignStatus.Sent && a.CampaignName.Contains(sText) || DbFunctions.TruncateTime(a.LaunchOn) == DbFunctions.TruncateTime(dt)).ToList();

Para obtener los resultados de la fecha a través de consultas Linq utilice esta función.