Need help with an error in LINQ expression that I use in MVC.net with C#.net code behind application

mainrotor
mainrotor used Ask the Experts™
on
Hi Experts,
I am getting an error in LINQ expression that I use in MVC.net with C#.net code behind application.
It's complaining about how I am handling a null date value.
I need help resolving this error

Here is the error:

An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

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


Here is my code:
            List<DataObject.HS> _lstTasks = new List<DataObject.HS>();
            using (CDO.CAEntities db = new CDO.CAEntities(new Security().getConnectionString(1), UserID, ID))
            {
                _lstTasks = (from m in db.HS
                             select new DataObject.HS
                             {
                                 ID = (int)m.ID,
                                 IssueDescription = m.IssueDescription,
                                 ResolvedDate = m.ResolveDate == null ? DateTime.Parse("1/1/1900") : (DateTime)m.ResolveDate,
                             }).ToList();
            }
            return _lstTasks;

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2015

Commented:
Move the DateTime.Parse out into a variable. Remember, EF tries to convert your LINQ code into SQL. Any expressions which doesn't have a compatible SQL equivalent will result in an error.
Retired
Distinguished Expert 2017
Commented:
Hi mainrotor;

I have modified your code as follows. Please try.
List<DataObject.HS> _lstTasks = new List<DataObject.HS>();
using (CDO.CAEntities db = new CDO.CAEntities(new Security().getConnectionString(1), UserID, ID))
{
    _lstTasks = (from m in db.HS
                 select new DataObject.HS
                 {
                     ID = (int)m.ID,
                     IssueDescription = m.IssueDescription,
                     ResolvedDate = m.ResolveDate == null ? new DateTime(1900, 1, 1) : (DateTime)m.ResolveDate,
                 }).ToList();
}
return _lstTasks;

Open in new window

Author

Commented:
AWESOME!!!  Thank you.
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Not a problem mainrotor, glad I was able to help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial