LINQ to Entities 在 DateTime.DayOfWeek 上加入

发布于 2024-09-15 03:56:54 字数 647 浏览 3 评论 0原文

想象两个表:班次、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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

荒芜了季节 2024-09-22 03:56:54

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/

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/

套路撩心 2024-09-22 03:56:54

看来我在这个级别已经无能为力了。因此,我所做的就是创建一个存储过程,该存储过程连接两个表并将其导入到实体中,创建一个返回 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.

无名指的心愿 2024-09-22 03:56:54
using System.Data.Objects.SqlClient; //Don't forget this!!

//You can access to SQL DatePart function using something like this:

YourTable.Select(t => new { DayOfWeek = SqlFunctions.DatePart("weekday", t.dateTimeField) - 1 }); //Zero based in SQL

//You can compare to SQL DatePart function using something like this:

DateTime dateToCompare = DateTime.Today;
YourTable.Where(t => SqlFunctions.DatePart("weekday", t.dateTimeField) - 1 == dateToCompare }); //Zero based in SQL
using System.Data.Objects.SqlClient; //Don't forget this!!

//You can access to SQL DatePart function using something like this:

YourTable.Select(t => new { DayOfWeek = SqlFunctions.DatePart("weekday", t.dateTimeField) - 1 }); //Zero based in SQL

//You can compare to SQL DatePart function using something like this:

DateTime dateToCompare = DateTime.Today;
YourTable.Where(t => SqlFunctions.DatePart("weekday", t.dateTimeField) - 1 == dateToCompare }); //Zero based in SQL
〆一缕阳光ご 2024-09-22 03:56:54

有趣的是,这在 Linq-to-Sql 中工作得很好:

from o in Orders
join c in Categories on (int) o.OrderDate.Value.DayOfWeek equals c.CategoryID
where o.OrderDate != null
select c

该查询毫无意义——它只是与正确数据类型的一些随机连接。 (我用的是北风)

Interestingly, this works fine in Linq-to-Sql:

from o in Orders
join c in Categories on (int) o.OrderDate.Value.DayOfWeek equals c.CategoryID
where o.OrderDate != null
select c

That query is meaningless -- It's just some random joins with the proper datatypes. (I was using Northwind)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文