Click here to Skip to main content
15,905,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have problem in this code
C#
using (IERPEntities iERPEntities = new IERPEntities())
        {
            var result = from res in iERPEntities.Item_General
                         where Convert.ToDateTime(res.Date) <= Convert.ToDateTime(stDate)
                              && Convert.ToDateTime(res.Date) >= Convert.ToDateTime(enDate)
                          && res.Name.Contains(txtSelectItemName.Text)
                         select res;
            GridView2.DataSource = result;
            GridView2.DataBind();
        }


this throw an exception which is
LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' method, and this method cannot be translated into a store expression.
Posted

1 solution

Hi

Long back I too faced the same problem

Instead of doing the any kind of casting inside your LINQ Query, do the casting first and assign it into any variable and use that variable inside the LINQ Query.

So your code need to be altered like this

DateTime dtStart = Convert.ToDateTime(stDate);
        DateTime dtEnd = Convert.ToDateTime(enDate);

        using (IERPEntities iERPEntities = new IERPEntities())
        {
            var result = from res in iERPEntities.Item_General
                        where res.Date <= dtStart
                        && res.Date >= dtEnd
                        && res.Name.Contains(txtSelectItemName.Text)
                        select res;
            GridView2.DataSource = result;
            GridView2.DataBind();
        }


I hope it will give you some idea regarding the problem.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900