1

I am trying to convert a datetime field to a string. The error message I got is found below. Have you seen this before? I am not getting a solution. How can I resolve this please?

            public IEnumerable<SelectListItem> GetStatusDate(string strprojectId)
                    {
                        var queryResult = (from dt in _objContext.tbl_Project_Status_MSTR
                            where dt.ProjectID.Equals(strprojectId)
                            orderby dt.ProjectID
                            select new SelectListItem {Text = Convert.ToString(dt.StatusDate),Value = Convert.ToString(dt.StatusDate)});
                        List<SelectListItem> objStatDate = queryResult.ToList();

                        return objStatDate;
                    }

LINQ to Entities does not recognize the method 'System.String ToString(System.DateTime)' method, and this method cannot be translated into a store expression.

1
  • You can use dt.StatusDate.ToString() which will be converted to expression. Commented May 11, 2016 at 19:28

3 Answers 3

3

One way to do it is to query StatusDate from the database, and then convert it to string in memory like this:

var objStatDate =
    (from dt in _objContext.tbl_Project_Status_MSTR
    where dt.ProjectID.Equals(strprojectId)
    orderby dt.ProjectID
    select new {dt.StatusDate})
    .ToList() //Execute database query
    .Select(x =>
        new SelectListItem
        {
            Text = Convert.ToString(x.StatusDate), //Convert data in memory
            Value = Convert.ToString(x.StatusDate)
        })
    .ToList();
Sign up to request clarification or add additional context in comments.

5 Comments

Big thanks to everyone. It works now with the solution above. Appreciate it.
Will I be able to cut off the time from the code above?
Yes. I think you can do it both at the database side (there are special functions that translate to SQL functions (see DbFunctions)) and at the local side by using x.StatusDate.Date.
Thanks. will try that.
If you don't want to display time when you convert, you can use x.StatusDate.ToString(.. (see the documentation here) with a special formatting string that does not include the time.
0

EF doesn't have a corresponding database function for Convert.ToString(), so it spits up that error.

The easiest way would be to do something like this, that first hits the database to only get the date, then converts to a string once it's all in memory as a normal collection.

                    var queryResult = (from dt in _objContext.tbl_Project_Status_MSTR
                        where dt.ProjectID.Equals(strprojectId)
                        orderby dt.ProjectID
                        select dt.StatusDate)
                        .AsEnumerable()
                        .Select(d => new SelectListItem {Text = Convert.ToString(d),Value = Convert.ToString(d)});

Although I might recommend using d.ToString() instead of convert, especially since you can use string formatting and stuff that way. You could also just slap a .ToList() onto the end of the query and return that, instead of doing it on the next line.

Comments

0

Use this

List<string> ListOfDateString = (from dt in ListOfDateTime let str=dt.ToString() select str).ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.