1

次のLINQクエリがありますが、staffIdでグループ化するように変更し、各staffIdのObservationDateがMaxであるレコードのみを選択します。

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
};
4

2 に答える 2

1
var observations = 
    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 result = from o in observations
             group o by o.staffId into g
             select g.OrderByDescending(x => x.observationDate).First();
于 2013-03-21T11:09:51.253 に答える