I am trying to get the latest record for the logged in employee in my HolidayRequestForm
table.
However based on advice from LINQ To Entities does not recognize the method Last. Really? I want to orderbydescending and select the first.
I've tried adding in orderbydescending but I get an error
"Error 3 'System.Data.TypedTableBaseExtensions.OrderByDescending(System.Data.TypedTableBase, System.Func, System.Collections.Generic.IComparer)' is a 'method', which is not valid in the given context
"
Do I have it in the wrong place?
var SD = (from c in db.HolidayRequestForms.OrderByDescending
where (c.Employee.Email == name) && (c.Employee.EmployeeID == c.EmployeeID)
select c.StartDate);
DateTime StartDate = SD.LastOrDefault();
I would like StartDate
to give the latest result in the HolidayRequestForm
table for the current logged in employee
db.HolidayRequestForms.OrderByDescending
doesn't make sense for two reasons.
()
after it)I'd suggest this as a replacement:
var SD = (from c in db.HolidayRequestForms where (c.Employee.Email == name) && (c.Employee.EmployeeID == c.EmployeeID)
select c.StartDate).OrderByDescending(z => z);
or:
var SD = db.HolidayRequestForms
.Where(c => c.Employee.Email == name && c.Employee.EmployeeID == c.EmployeeID)
.OrderByDescending(z => z.StartDate)
.Select(y => y.StartDate);
You will also want to use FirstOrDefault
rather than LastOrDefault
.