c# - LINQ Query for GroupBy and Max in a Single Query -


i have following linq query want modify want group staffid , pick records observationdate max each staffid.

from ob in db.tdtobservations.oftype<tdtspeedobservation>() select new {    id = ob.id,    academicyearid = ob.teachers.firstordefault().classes.firstordefault().academicyearid,    observationdate = ob.observationdate,    schoolid = ob.teachers.firstordefault().classes.firstordefault().schoolid,    staffid=ob.teachers.firstordefault().id }; 

var observations =      ob in db.tdtobservations.oftype<tdtspeedobservation>()     select new  {        id = ob.id,        academicyearid = ob.teachers.firstordefault().classes.firstordefault().academicyearid,        observationdate = ob.observationdate,        schoolid = ob.teachers.firstordefault().classes.firstordefault().schoolid,        staffid=ob.teachers.firstordefault().id     };  var result = o in observations              group o o.staffid g              select g.orderbydescending(x => x.observationdate).first(); 

Comments