Microsoft Developer Network > Forums Home > Archived Forums Forums > ADO.NET Entity Framework and LINQ to Entities (Pre-Release) > Does the new EF 4.0 still not support DateTime functions like DayOfWeek?

Answered Does the new EF 4.0 still not support DateTime functions like DayOfWeek?

  • Thursday, July 23, 2009 6:55 PM
     
     
    System.NotSupportedException: The specified type member 'DayOfWeek' is not supported in LINQ to Entities.

    Seems odd that it still doesn't have parity with Linq-to-Sql yet??

Answers

  • Friday, July 24, 2009 4:33 AM
    Moderator
     
     Answered

    Hello,

    We currently don't have plans to support DayOfWeek. As Zeeshan mentions, you can use SqlFunctions.DatePart.

    We understand that supporting DayOfWeek in LINQ to Entities would be nice, but we had to decide among many features and this one didn't make it, so we keep it in a list of capabilities we would like to add in the future.

    Thanks,
    Diego


    This posting is provided "AS IS" with no warranties, and confers no rights.

All Replies

  • Thursday, July 23, 2009 11:23 PM
     
     
    with beta 1, there is a new class SqlFunctions that exposes DatePart function which will allow you to do what you want. Caution that the number returned is sql server specific meaning day of the week for sql server is 1-7 and day of week in C# is 0-6

    Zeeshan Hirani
  • Thursday, July 23, 2009 11:58 PM
     
     
    Thanks.

    But in Linq-to-Sql I can just write ...            

        this.dayOfWeekCondition = (dt => dt.DayOfWeek == dayOfWeek);

    and everything works just fine.  The conversion of the Expression tree to SQL is handled for me and I can USE THE SAME predicate expression whether I'm using CLR classes, SQL server, ....  I believe it handles the 1-7 vs 0-6 issue just fine too also although I haven't inspected the code it generates.

    So why can't the Entity framework handle the conversion for me just like Linq-To-Sql does?

    It seems somewhat cumbersome and a big step backwards if I now have to return different expression trees depending on what database is being targeted, or maybe build my own conversion code that I insert when accessing a SQL database to convert expressions that Linq-to-Sql handled just fine into ones that use the new SqlFunctions methods.

  • Friday, July 24, 2009 4:33 AM
    Moderator
     
     Answered

    Hello,

    We currently don't have plans to support DayOfWeek. As Zeeshan mentions, you can use SqlFunctions.DatePart.

    We understand that supporting DayOfWeek in LINQ to Entities would be nice, but we had to decide among many features and this one didn't make it, so we keep it in a list of capabilities we would like to add in the future.

    Thanks,
    Diego


    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Thursday, August 04, 2011 4:36 PM
     
     
    You are a life saver. Never knew you could use the SQL DATEPART function directly in Linq. You saved me a lot of work.