简体   繁体   English

LINQ to Entities无法识别方法'System.DateTime ToDateTime(System.String)'方法

[英]LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' method

I am trying to convert one application to EntityFrameWork codefirst. 我试图将一个应用程序转换为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 LINQ to Entities无法识别方法'System.DateTime ToDateTime(System.String)'方法

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. LINQ to Entities无法将大多数.NET Date方法(包括您使用的转换)转换为SQL,因为没有等效的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, 但等待上面的查询将获取整个数据 ,并执行.Where在它上面,绝对你不希望这样,

simple soultion would be this , personally, I would have made my Entity field as DateTime and db column as DateTime 简单的灵魂就是这个 ,就个人而言,我会把我的Entity字段作为DateTime ,将db列作为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 但是,因为你的db / Entity Date字段是字符串,除了将实体和db中的字段更改为DateTime然后进行比较之外,你没有任何其他选项

Why is your date column a string? 为什么您的日期列是字符串? Shouldn't it be a DateTime ? 不应该是DateTime吗?

Regardless, if you attempt to perform conversions using .NET functions in a .Where statement against a repository of entities, you'll get that error. 无论如何,如果您尝试在针对实体存储库的.Where语句中使用.NET函数执行转换,您将收到该错误。

Your best option would be to change that column from a string to a DateTime and proceed from there. 您最好的选择是将该列从字符串更改为DateTime并从那里继续。 If you do, the .Where statement would be: 如果你这样做, .Where语句将是:

DateTime dtFrom = Convert.ToDateTime(sFrom );
var something = TransRepository.Entities.Where(x  => x.Date >= dtFrom) ;

/ i had a similar problem where i was getting a search string and querying a datetime column... see line 4 / / 我有一个类似的问题,我得到一个搜索字符串,并查询日期时间列...见第4行 /

1)case "Admission_Date":
2)if (!String.IsNullOrEmpty(SearchValue))
3)   {
4)   DateTime dtFrom = 
Convert.ToDateTime(SearchValue); 
wards = from s in db.CORE_DATA_tbl
    where s.Admit_Date == dtFrom
    orderby s.ActionType, s.AdmissionLocation
    select s;
    }
    break;

Take the convert part out of linq statement and keep it in a variable, like this: 从linq语句中取出转换部分并将其保存在变量中,如下所示:

var xyz = Convert.ToDateTime("12/31/2018");

and use this variable in the linq statement. 并在linq语句中使用此变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LINQ to Entities无法识别方法'System.DateTime ToDateTime(System.String)' - LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' LINQ to Entities无法识别方法'System.DateTime ToDateTime(System.String)' - LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' ASP.NET实体框架LINQ to Entities无法识别方法'System.DateTime ToDateTime(System.String) - ASP.NET Entity Framework LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String) LINQ to Entities无法识别方法ToDateTime(System.String) - LINQ to Entities does not recognize the method ToDateTime(System.String) LINQ to Entities无法识别方法'System.DateTime ConvertShamsiToMiladi(System.String)'方法 - LINQ to Entities does not recognize the method 'System.DateTime ConvertShamsiToMiladi(System.String)' method LINQ to Entities无法识别MVC5中的方法'System.DateTime ParseExact(System.String,System.String,System.IFormatProvider)'方法错误 - LINQ to Entities does not recognize the method 'System.DateTime ParseExact(System.String, System.String, System.IFormatProvider)' method error in MVC5 LINQ to Entities 无法识别方法“System.DateTime GetValueOrDefault()” - LINQ to Entities does not recognize the method 'System.DateTime GetValueOrDefault()' LINQ to Entities 无法识别“System.String Decrypt(System.String, System.String)”方法 - LINQ to Entities does not recognize the method 'System.String Decrypt(System.String, System.String)' method LINQ to Entities无法识别方法'System.String getFullname(System.String,System.String)' - LINQ to Entities does not recognize the method 'System.String getFullname(System.String, System.String)' LINQ to Entities无法识别方法'System.String ToString()'方法++++++ - LINQ to Entities does not recognize the method 'System.String ToString()' method ++++++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM