sumanthb
sumanthb

Reputation: 25

why the date conversion is not working in linq

I am new to mvc i had problem with following code it is throwing an exception: LINQ to Entities does not recognize the method 'System.String ToString(System.DateTime)' method, and this method cannot be translated into a store expression.

     public ActionResult JobSearchList(PostJobModel model)
     {
         try
         {

         if (Session["USER_ID"] != null)
         {
             var subscriber_Id = RL_Constants.RES_ID;
             var company_Id = RL_Constants.COMP_ID;
             var resource_Typa = RL_Constants.RES_TYPE;
             var jobPostDetails = (from jobPost in reslandentity.JOB_POSTING
                                   where jobPost.COMP_ID == RL_Constants.COMP_ID
                                   select new PostJobModel 
                                   {  
                                       POST_DT=Convert.ToString(jobPost.POST_DT),
                                       POST_END_DT=Convert.ToString(jobPost.POST_END_DT),
                                       POSITIONS_CNTS=Convert.ToString(jobPost.POSITIONS_CNT),
                                       JOB_TYPE = jobPost.JOB_TYPE,
                                       SKILLS = jobPost.SKILLS,
                                       DURATION = jobPost.DURATION,
                                       CATEGORY=jobPost.CATEGORY,
                                       PREREQUISITES = jobPost.PREREQUISITES,
                                       LOCATION=jobPost.LOCATION,
                                       RATE=jobPost.RATE,
                                       PERKS = jobPost.PERKS,
                                       CONTACT_PERSON=jobPost.CONTACT_NAME,
                                       CONTACT_EMAIL=jobPost.CONTACT_INFO,
                                       CONTACT_PHONE=jobPost.CONTACT_INFO,
                                       POST_TITLE=jobPost.TITLE,  
                                       DESCRIPTION=jobPost.DESCR                                                                                                                                                                    
                                   }).ToList();
             model.GetJobPostDetails = jobPostDetails;



         }

         return View(model);
     }
        catch (Exception ex)
        {

            throw ex;
        }

     }

Upvotes: 1

Views: 543

Answers (2)

James
James

Reputation: 82096

The error is pretty self explantory, ToString is not supported by the LINQ provider therefore you can't use it in a query.

Pull down the raw data and perform your conversions in memory.

var jobPostDetails = reslandentity.JOB_POSTING
   .Where(x => x.COMP_ID == RL_Constants.COMP_ID)
   .AsEnumerable() // materialize query, Select will be performed in memory
   .Select(x => new {
       POST_DT = x.POST_DT.ToString(),
       POST_END_DT = x.POST_END_DT.ToString(),
       POSITIONS_CNTS = x.POSITIONS_CNT.ToString(),
       ...
   })
   .ToList();

AsEnumerable will switch the context from IQueryable<T> to IEnumerable<T> therefore allows for CLI-specific methods to be called as part of the query. The query will still only be materialized after ToList is called as AsEnumerable retains delayed execution.

Upvotes: 1

Johannes Wanzek
Johannes Wanzek

Reputation: 2875

Take a look at SqlFunctions when trying to convert something inside a LINQ Query.

SqlFunctions.DateName("dd", jobPost.POST_DT) + 
SqlFunctions.DateName("mm", jobPost.POST_DT) + 
SqlFunctions.DateName("yyyy", jobPost.POST_DT) + 

This functions can also be used inside where clauses etc.

Upvotes: 1

Related Questions