I’m pretty new still at C# so I might be doing something stupid, but I’ve spent some time looking at this and still can’t see what the problem is.
Here are some code snippets:
double work = 0;
ProjectRepository pr = new ProjectRepository();
IQueryable<CalendarDetail> cds;
// Find matching day of week
// Then for that day, cycle through all working times
// Return list of working times in day
cds = pr.GetCalDetails(calendarID, startTime.DayOfWeek.GetHashCode());
foreach (CalendarDetail cd in cds)
{
DateTime wts = startTime.Date + cd.WorkingTimeStart.Value.TimeOfDay;
DateTime wtf = startTime.Date + cd.WorkingTimeFinish.Value.TimeOfDay;
//more code here....
if ((cds.Last().CalendarDetailID == cd.CalendarDetailID) && (finishTime > wtf))
work += Work(startTime.Date.AddDays(1), finishTime, calendarID);
}
The error is being thrown run-time due to my use of the cds.Last() method call. However, cds has been declared and is being used as an IQueryable object, so what is the problem?
Error text:
The query operator ‘Last’ is not supported.
Failing a solution I’m sure I can ‘logic’ my way around the problem, but this seemed elegant.
Thanks,
Jonathan
I don’t think there’s enough detail in this question to answer it, because you don’t indicate what the exception actually was.
It might be worth replacing the call to .Last() to .AsEnumerable().Last(), as this would indicate whether the problem was with Last() at all, or if it was elsewhere in the query.
.AsEnumerable().Last() can be slower and can’t be faster so it’s not worth doing generally if you can avoid it, but (A) maybe you can’t because of some limitation in the query provider and (B) the result of trying it and seeing if you get the same exception will tell you more about your problem.