Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to Entities Join on DateTime.DayOfWeek

Imagine two tables: Shifts, RANK_S_DAY. Shifts contains a ShiftDate column which is DateTime and RANK_S_DAY has a DayOfWeek column. I need to join (int)ShiftDate.DayOfWeek equals DayOfWeek. I understand why it won't work, but I'm not quite sure how I can change it. The Exception is:

The specified type member 'DayOfWeek' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

As I understand it, LINQ can't translate (int)ShiftDate.DayOfWeek to something SQL understands, Any ideas?

Here is the code:

Shifts = from s in en.Shifts
join j in en.RANK_S_JOB on s.kronos_JobPositions.JobID equals j.JOBNO
join d in en.RANK_S_DAY on (int)s.ShiftDate.DayOFWeek equals d.DAY_OF_WEEK
orderby
 d.RANK,
 j.RANK ascending
select s;
like image 619
Steve Avatar asked Aug 18 '10 23:08

Steve


1 Answers

LINQ to SQL

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

LINQ to Entities

int dow = (int)dayOfWeek + 1; // SQL Day of week
var dayOfWeekCondition = (dt => SqlFunctions.DatePart(“weekday”, dt) == dow);

Source:

http://blog.abodit.com/2009/07/entity-framework-in-net-4-0/

like image 149
Leniel Maccaferri Avatar answered Oct 19 '22 03:10

Leniel Maccaferri