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

前端 未结 4 497
再見小時候
再見小時候 2020-12-15 13:36

I am trying to convert one application to EntityFrameWork codefirst. My present code is

 string sFrom  =\"26/12/2013\";
 select * FROM Trans where  CONVERT(d         


        
4条回答
  •  -上瘾入骨i
    2020-12-15 14:11

    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

提交回复
热议问题