Linq to Entities SqlFunctions.DatePart

This code throws an error:

void GetLog()
{
    List<CAR_STATUS_LOGS> logs = null;
    using (TESTEntities ctx = new TESTEntities())
    {
        logs = 
            ctx.CAR_STATUS_LOGS
                .Where(a => SqlFunctions.DatePart("DAY", a.TIMEMARK) == 1)
                .ToList();
    }
}

Error:

LINQ to Entities does not recognize the method 'System.Nullable`1 [System.Int32] DatePart (System.String, System.Nullable`1 [System.DateTime])', and this method cannot be translated into a stored expression.

I do not see what I am doing wrong with SqlFunctions.DatePart. The TIMEMARK column is a type of Sqlserver DateTime.
Any ideas?

+4
source share
4 answers

Linq to Entites does not support SqlFunctions.

if a.TIMEMARK is a date, you cannot access the day with .TIMEMARK.Day

-2
source

System.Data.Entity.SqlServer.SqlFunctions EntityFramework.SqlServer.dll(Entity Framework 6.1.3), :

myTable.Where (w => 
    (System.Data.Entity.SqlServer.SqlFunctions.DatePart("weekday", w.Data).Value == 1);
+13
DbFunctions.DiffDays(Date1,Date2)==1
0
source

SqlFunctions.DatePart("day", a.TIMEMARK) == 1).ToList();

dd, d

for more details: -

http://www.w3schools.com/sql/func_datepart.asp

-1
source

Source: https://habr.com/ru/post/1548488/


All Articles