2012-04-26 9 views
5

tengo el siguiente códigoLINQ a Entidades no SqlFunctions.DateDiff apoyado

DateTime now = DateTime.UtcNow; 

var allItemsOver64 = _inventoryContext.Items.Where(i => 
    (SqlFunctions.DateDiff("dd", i.PrimaryInsured.DoB, now)/365.0) >= 65); 

IQueryable<Item> items65To69 = allItemsOver64.Where(i => 
    (SqlFunctions.DateDiff("dd", i.PrimaryInsured.DoB, now)/365.0) >= 65 && 
    (SqlFunctions.DateDiff("dd", i.PrimaryInsured.DoB, now)/365.0) <= 69); 

Pero cuando intento y utilizo tanto allItemsOver64 Items65To69.Count() me sale este error

La expresión ((((Convertir (DateDiff ("dd", [10007] .PrimaryInsured.DoB, 26/04/2012 15:03:09))/365)> = 65) Y ((Convert (DateDiff ("dd") , [10007] .PrimaryInsured.DoB, 26/04/2012 15:03:09))/365)> = 65)) Y ((Convert (DateDiff ("dd", [10007] .PrimaryInsured.DoB, 26/04/2012 15:03:09))/365) < = 69)) no es compatible.

¿Qué estoy haciendo mal?

+0

Gracias por la edición Arion. –

+0

¿Qué sistema RDBMS estás usando? –

Respuesta

6

Quizás podría tratar de usar EntityFunctions en lugar de SqlFunctions.

+0

Ahora obtengo La expresión (((DiffYears ([10007] .PrimaryInsured.DoB, 26/04/2012 15:25:49)> = 65) Y (DiffYears ([10007] .PrimaryInsured.DoB, 26/04 2012 15:25:49)> = 65)) Y (DiffYears ([10007] .PrimaryInsured.DoB, 26/04/2012 15:25:49) <= 69)) no es compatible. –

+0

Hoy en día DbFunctions; EntityFunctions está obsoleto –

+0

@ ThomasHagström - ¿Por qué diría que está obsoleto? Si revisa la documentación, puede ver que todavía está incluida en el marco y definitivamente hay operaciones soportadas allí que no son compatibles en ningún otro lado. –

2

función DateDiff con LINQ a la Entidad está siguiendo abajo

Si desea

Day difference 
EntityFunctions.DiffDays(startDate , endDate); 

Hours diff -> 
EntityFunctions.DiffHours(startDate , endDate); 

Minuutes diff -> 
EntityFunctions.DiffMinutes(startDate , endDate);