LINQ到实体无法识别方法“的System.DateTime ToDateTime(System.String)”方法 [英] LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' method
问题描述
我想一个应用程序转换的EntityFramework codefirst。
我现在的代码是
字符串sFrom =26/12/2013;
SELECT * FROM横贯其中,CONVERT(日期时间,日期,105)GT = CONVERT(日期时间,'+ sFrom +',105)
和我试图
的DateTime dtFrom = Convert.ToDateTime(sFrom);
TransRepository.Entities.Where(X => Convert.ToDateTime(x.Date)> = dtFrom)
但我得到这样
错误
LINQ到实体无法识别方法'System.DateTime的
ToDateTime(System.String)方法
块引用>
请帮忙提前
由于< DIV CLASS =h2_lin>解决方案
当你这样做:
TransRepository.Entities.Where (X => Convert.ToDateTime(x.Date)> = dtFrom)
LINQ到实体因为没有相应的SQL无法转换大多数.NET日期方法(包括您使用的是铸)成SQL。
你需要做的是下面的事:
的DateTime dtFrom = Convert.ToDateTime(sFrom);
TransRepository
.Entities.ToList()//部队执行
。凡(X => Convert.ToDateTime(x.Date)> = dtFrom)
但等待上面的查询将获取的整个数据,然后执行。凡
就可以了,你肯定不希望出现这种情况,
简单soultion会的这个,
个人而言,我会作出我的实体字段作为日期时间
和DB列的DateTime
不过,因为你的DB /实体日期
字段是字符串,你没有任何其他选择,除了改变实体和分贝的DateTime $ C你的领域$ C>,然后做的比较
I am trying to convert one application to EntityFrameWork codefirst. My present code is
string sFrom ="26/12/2013";
select * FROM Trans where CONVERT(datetime, Date, 105) >= CONVERT(datetime,'" + sFrom + "',105)
And i tried
DateTime dtFrom = Convert.ToDateTime(sFrom );
TransRepository.Entities.Where(x =>Convert.ToDateTime(x.Date) >= dtFrom)
But I got an error like this
LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' method
Please help Thanks in advance
when you do this:
TransRepository.Entities.Where(x =>Convert.ToDateTime(x.Date) >= dtFrom)
LINQ to Entities cannot translate most .NET Date methods (including the casting you used) into SQL since there is no equivalent SQL. What you need to do is to do below:
DateTime dtFrom = Convert.ToDateTime(sFrom );
TransRepository
.Entities.ToList()//forces execution
.Where(x =>Convert.ToDateTime(x.Date) >= dtFrom)
but wait the above query will fetch entire data, and perform .Where
on it, definitely you don't want that,
simple soultion would be this,
personally, I would have made my Entity field as DateTime
and db column as DateTime
but since, your db/Entity Date
field is string, you don't have any other option, other than to change your field in the entity and db to DateTime
and then do the comparison
这篇关于LINQ到实体无法识别方法“的System.DateTime ToDateTime(System.String)”方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!