Linq to sql datetime issue

When I am calling GetClosestStartDate it is popingup the following message Additional information:

LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression. I also tried by removing ToString from "TT.Startdate.Value.ToString()" and Changing "string CStartdate { get; set; } to DateTime CStartdate { get; set; }"

class Class_CourseStartdate
{
    public string CStartdate { get; set; }
    public int TimeTableID { get; set; }
}

   public List<Class_CourseStartdate> GetClosestStartDate(DateTime CoeStartdate)
   {
       return (from TT in _er.TimeTables
               where TT.Startdate == CoeStartdate
               select new Class_CourseStartdate 
               { CStartdate = TT.Startdate.Value.ToString() ,
                 TimeTableID = TT.TimeTableID 
               }
               ).ToList();
   }
Jon Skeet
people
quotationmark

The simplest approach is to fetch just the data you want in the part that's translated into SQL, and then do the rest locally:

public List<Class_CourseStartdate> GetClosestStartDate(DateTime coeStartdate)
{
    return _er.TimeTables
              .Where(tt => tt.Startdate == coeStartdate)
              .Select(tt => new { tt.Startdate, tt.TimeTableID })
              .AsEnumerable() // Do the rest locally
              .Select(tt => new Class_CourseStartdate 
              { 
                   CStartdate = tt.Startdate.Value.ToString() ,
                   TimeTableID = tt.TimeTableID 
              }
              .ToList();
}

Note that I've stopped using a query expression here, as it becomes ugly when you need to then call methods on it - and all you're doing is a simple Where and Select. Query expressions are most useful when they involve transparent identifiers, e.g. in joins.

Next up, I would notice that we're already checking that tt.Startdate is equal to the date we've passed in - so why bother fetching it?

public List<Class_CourseStartdate> GetClosestStartDate(DateTime coeStartdate)
{
    return _er.TimeTables
              .Where(tt => tt.Startdate == coeStartdate)
              .Select(tt => tt.TimeTableID)
              .AsEnumerable() // Do the rest locally
              .Select(tid => new Class_CourseStartdate 
              { 
                   CStartdate = coeStartDate.ToString() ,
                   TimeTableID = tid
              }
              .ToList();
}

Or possibly just change the method to return timetable IDs, given that that's really what it's doing. I'd change the method name too:

public List<int> GetTimeTableIdsForStartDate(DateTime coeStartdate)
{
    return _er.TimeTables
              .Where(tt => tt.Startdate == coeStartdate)
              .Select(tt => tt.TimeTableID)
              .ToList();
}

Even if you do stick with Class_CourseStartdate I would change the CStartdate property to be just DateTime rather than string - avoid using strings in your models unless you really, really have to.

Additionally, your naming currently feels like it's all over the place - Startdate should be StartDate everywhere, you should drop the C and Class_ prefixes, etc.

people

See more on this question at Stackoverflow