locked
'DayOfWeek' is not supported in LINQ to Entities.. RRS feed

  • Question

  • User782238218 posted

    Hi, The following code....

            IEnumerable<CalendarDate> LessonDates = db.CalendarDates

                  .Where(cd => cd.Date.DayOfWeek == DayOfWeek.Friday);

    ....generates an error message "The specified type member 'DayOfWeek' is not supported in LINQ to Entities"

    This is somewhat surprising, considering that Intellisense supplied DayOfWeek as an option!

    What is the best workaround? Do I have to use Linq to Sql?

    regards, Guy

    Saturday, November 16, 2013 10:11 AM

Answers

  • User281315223 posted

    Typically when you attempt to use a property that LINQ doesn't natively support, you'll need to create a concrete implementation of the collection before applying your LINQ constraints.

    You can do this using the ToList() or AsEnumerable() methods prior to your Where clause as seen below : 

    //Using the ToList() method
    IEnumerable<CalendarDate> LessonDates = db.CalendarDates.ToList().Where(cd => cd.Date.DayOfWeek == DayOfWeek.Friday);
    
    //Using the AsEnumerable() method
    IEnumerable<CalendarDate> LessonDates = db.CalendarDates.AsEnumerable().Where(cd => cd.Date.DayOfWeek == DayOfWeek.Friday);
    
    

    Typically, using your previous method, LINQ would try to generate a query that relied on the DayOfWeek property which SQL doesn't natively support.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 16, 2013 12:00 PM

All replies