How to resolve casting error from type system.datetime to type system.iconvertible?

Hi Experts,

The following code results in an error:

ABCEntities Context = new ABCEntities();

                var woDateQuery = (from p in Context.WOes
                                  where p.WorkOrder == int.Parse(workOrderDropDownList.SelectedValue.ToString())
                                  select p.Date);
                
                dateRequestedLabel.Text = Convert.ToString(woDateQuery);

Open in new window


p.Date is a DB Type datetime.  What I'm trying to do is convert a datetime variable into a string.

The error I get is as follows: NotSupportedException was unhandled by user code.  LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression.

Thanks for your help.

W
WhyDidntItWorkAsked:
Who is Participating?
 
Kyle AbrahamsSenior .Net DeveloperCommented:
The issue is with the int.Parse in your linq expression.  Not the datetime.  The workaround is to parse it before hand and pass the value in.
int val  = int.Parse(workOrderDropDownList.SelectedValue.ToString());

                var woDateQuery = (from p in Context.WOes
                                  where p.WorkOrder == val
                                  select p.Date);

Open in new window

0
 
WhyDidntItWorkAuthor Commented:
Thanks for the help.  Much appreciated.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.