Я хочу сгруппировать по полям в tblInspection и выбрать самую последнюю InspectionDate в tblInspectionDate, но я не знаю, как ссылаться на поле в таблице «Включено».
public IEnumerable<InspectionEvent> GetInspectionEvents() //string facilityID)
{
using (var context = new FacilityEntities())
{
var inspections = context.tblInspection.AsNoTracking().Include("tblInspectionDate");
List<InspectionEvent> inspectionList =
(from insp in inspections
group insp by new { insp.InspectionID, insp.Inspection, insp.Inspector, insp.FacilityID, insp.InventoryID, insp.Period }
into g
select new InspectionEvent
{
InspectionID = g.Key.InspectionID,
InspectionName = g.Key.Inspection,
Inspector = g.Key.Inspector,
FacilityID = g.Key.FacilityID,
InventoryID = g.Key.InventoryID,
Period = g.Key.Period,
InspectionDate = g.Max(x => x.tblInspectionDate.InspectionDate? something like this?)
}).ToList();
return inspectionList;
}
}
Вот tblInspectionкласс:
public partial class tblInspection
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public tblInspection()
{
this.tblInspectionDate = new HashSet<tblInspectionDate>();
}
public int InspectionID { get; set; }
public string Inspection { get; set; }
public string Inspector { get; set; }
public Nullable<int> FacilityID { get; set; }
public Nullable<int> InventoryID { get; set; }
public string Period { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<tblInspectionDate> tblInspectionDate { get; set; }
}
И класс tblInspectionDate:
public partial class tblInspectionDate
{
public int InspectionID { get; set; }
public System.DateTime InspectionDate { get; set; }
public int UserID { get; set; }
public virtual tblInspection tblInspection { get; set; }
public virtual tblUser tblUser { get; set; }
}