Search code examples
c#linq-to-entities

LINQ group by with included table


I want to group by fields in tblInspection and select the most recent InspectionDate in tblInspectionDate, but I don't know how to refer to the field in the Included table.

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

Here is the tblInspection class:

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

And the tblInspectionDate class:

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

Solution

  • You don't need to group. I think it's safe to assume that InspectionID is Inspection's primary key, so a group always contains one Inspection, which makes grouping pointless. What you want can relatively simply be queried by:

    from insp in inspections
    select new InspectionEvent
    {
        InspectionID = insp.InspectionID,
        InspectionName = insp.Inspection,
        Inspector = insp.Inspector,
        FacilityID = insp.FacilityID,
        InventoryID = insp.InventoryID,
        Period = insp.Period,
        InspectionDate = (DateTime?)insp.tblInspectionDate.Max(d => d.InspectionDate)
    }
    

    The cast to DateTime? is to account for inspections without any tblInspectionDate.

    Side note: remove these tiring tbl prefixes from class and property names and use plural names for collections.