Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to Entities SqlFunctions.DatePart

This code is producing 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();
    }
}

The error is this:

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

I can't see what I am doing wrong with the SqlFunctions.DatePart. The column TIMEMARK is a Sqlserver DateTime type.
Any ideas?

like image 540
Paul Avatar asked Dec 11 '22 04:12

Paul


1 Answers

You can use System.Data.Entity.SqlServer.SqlFunctions from assembly EntityFramework.SqlServer.dll (Entity Framework 6.1.3), example:

myTable.Where (w => 
    (System.Data.Entity.SqlServer.SqlFunctions.DatePart("weekday", w.Data).Value == 1);
like image 111
Alexandre Avatar answered Dec 15 '22 03:12

Alexandre