LINQ to Entities Exception中不支持指定的类型成员'Date' [英] The specified type member 'Date' is not supported in LINQ to Entities Exception
问题描述
DateTime结果;
if(!DateTime.TryParse(rule.data,out result))
返回jobdescriptions;
if(result< new DateTime(1754,1,1))// sql无法处理1-1-1753之前的日期
返回jobdescriptions;
return jobdescriptions.Where(j => j.JobDeadline.Date == Convert.ToDateTime(rule.data).Date);
异常
LINQ to Entities不支持指定的类型成员'Date'。只支持初始化器,实体成员和实体导航属性。
我知道异常是什么意思,但我不知道如何摆脱它。任何帮助?
LINQ to Entities无法将大多数.NET Date方法(包括您使用的转换)转换为SQL,因为有没有等价的SQL。
解决方案是使用LINQ语句之外的Date方法,然后传入一个值。它看起来好像Convert.ToDateTime(rule.data).Date导致错误。
DateTime属性上的调用日期也不能转换为SQL,所以解决方法是比较.Year.Month和.Day属性,可以转换为LINQ,因为它们只是整数。
var ruleDate = Convert.ToDateTime(rule.data).Date;
return jobdescriptions.Where(j => j.Deadline.Year == ruleDate.Year
&& j.Deadline.Month == ruleDate.Month
&& j .Deadline.Day == ruleDate.Day);
I got a exception while implementing the following statements.
DateTime result;
if (!DateTime.TryParse(rule.data, out result))
return jobdescriptions;
if (result < new DateTime(1754, 1, 1)) // sql can't handle dates before 1-1-1753
return jobdescriptions;
return jobdescriptions.Where(j => j.JobDeadline.Date == Convert.ToDateTime(rule.data).Date );
Exception
The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
I know what the exception means but i don't know how to get rid of it. Any help?
LINQ to Entities cannot translate most .NET Date methods (including the casting you used) into SQL since there is no equivalent SQL.
The solution is to use the Date methods outside the LINQ statement and then pass in a value. It looks as if Convert.ToDateTime(rule.data).Date is causing the error.
Calling Date on a DateTime property also cannot be translated to SQL, so a workaround is to compare the .Year .Month and .Day properties which can be translated to LINQ since they are only integers.
var ruleDate = Convert.ToDateTime(rule.data).Date;
return jobdescriptions.Where(j => j.Deadline.Year == ruleDate.Year
&& j.Deadline.Month == ruleDate.Month
&& j.Deadline.Day == ruleDate.Day);
这篇关于LINQ to Entities Exception中不支持指定的类型成员'Date'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!