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


I am getting an error "System.NotSupportedException: LINQ to Entities does not recognize the method 'System.DateTime Parse(System.String)' method and this method cannot be translated into a store expression." when trying to run below code in C# ASP.NET MVC

var LastWeekDay = DateTime.Now.AddDays(-7);
            using (var context = new bwavenueEntities())
            {
               
                QuotesFeteched = context.QuoteTableMains.Where(a => Convert.ToDatetime(a.DateCreated) > LastWeekDay).Count();

            }

I am using Entity framework in my asp.net MVC project, how can I resolve it? Any idea or help?

Thanks


Asked by:- bhanu
0
: 552 At:- 3/13/2020 9:11:34 AM
C# entity-framework linq-to-entities






1 Answers
profileImage Answered by:- vikas_jk

Try using

var LastWeekDay = DateTime.Now.AddDays(-7);
            using (var context = new bwavenueEntities())
            {
               

                QuotesFeteched = context.Where(a => DateTime.Parse(a.DateCreated) > LastWeekDay).ToList();


            }

OR

it will be better if you convert your database columns of DateCreated into "datetime" type, instead of "string" type.

OR

  TransRepository
 .Entities.ToList()//forces execution
 .Where(x  =>Convert.ToDateTime(x.DateCreated) >= dtFrom) 

But above method will fetch entire data and will consume lots of memory.

1
At:- 3/17/2020 9:19:31 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use

Subscribe Now

Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly