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.
CStartdateastring?