sql – LINQ to Entities在DateTime.DayOfWeek上加入

前端之家收集整理的这篇文章主要介绍了sql – LINQ to Entities在DateTime.DayOfWeek上加入前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
想象一下两张桌子:Shifts,RANK_S_DAY.移位包含一个ShiftDate列,它是DateTime,RANK_S_DAY有一个DayOfWeek列.我需要加入(int)ShiftDate.DayOfWeek等于DayOfWeek.我理解为什么它不起作用,但我不确定如何改变它.例外情况是:

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

据我所知,LINQ无法将(int)ShiftDate.DayOfWeek翻译成sql理解的东西,任何想法?

这是代码

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;

解决方法

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);

资源:

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

猜你在找的MsSQL相关文章