LINQ to Entities 在 DateTime.DayOfWeek 上加入
想象两个表:班次、RANK_S_DAY。 Shifts 包含一个 ShiftDate 列(DateTime
),RANK_S_DAY 包含一个 DayOfWeek
列。我需要加入 (int)ShiftDate.DayOfWeek equals DayOfWeek
。我明白为什么它不起作用,但我不太确定如何改变它。例外是:
LINQ to Entities 不支持指定的类型成员“DayOfWeek”。仅支持初始值设定项、实体成员和实体导航属性。
据我了解,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;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
LINQ to SQL
LINQ to Entities
来源:
http://blog.abodit.com/2009/07/entity-framework-in-net-4-0/
LINQ to SQL
LINQ to Entities
Source:
http://blog.abodit.com/2009/07/entity-framework-in-net-4-0/
看来我在这个级别已经无能为力了。因此,我所做的就是创建一个存储过程,该存储过程连接两个表并将其导入到实体中,创建一个返回 Shifts 实体的函数导入。不确定这是否是最好的方法,但它有效并且干净。
It appears there isn't anything I can do at this level. So what I've done is created a stored proc that joins the two tables and imported it into the Entity, created a function import that returned a Shifts entity. Not sure if thats the best approach, but it works and is clean.
有趣的是,这在 Linq-to-Sql 中工作得很好:
该查询毫无意义——它只是与正确数据类型的一些随机连接。 (我用的是北风)
Interestingly, this works fine in Linq-to-Sql:
That query is meaningless -- It's just some random joins with the proper datatypes. (I was using Northwind)