Entity Framework in .NET 4
I recently tried the latest release of .NET 4.0 to see what improvements have been made to the Entity Framework. Overall it’s good news BUT I’m disappointed to see that things that worked in Linq-To-Sql still don’t work in Linq-To-Entities. For example, simple DateTime manipulation like checking the DayOfWeek doesn’t work.
In Linq-To-Sql you could use an expression like this:
var dayOfWeekCondition = (dt => dt.DayOfWeek == dayOfWeek);
But in Linq-To-Entities you have to use this expression:
int dow = (int)dayOfWeek + 1; // SQL Day of week
var dayOfWeekCondition = (dt => SqlFunctions.DatePart(“weekday”, dt) == dow);
AND by doing that you have now bound your expression to working only with SQL Server. If you want an expression that works with CLR types you’ll need to build that separately.
If anyone builds the code to automatically convert an Expression tree to SqlFunction calls from regular C# expressions do let me know!
[Update: The final release of .NET 4 includes fixes for most of these issues]
Comments are closed.