簡體   English   中英

LINQ group by 包含表

[英]LINQ group by with included table

我想按tblInspection中的字段分組,並在tblInspectionDate中選擇最近的InspectionDate,但我不知道如何引用Included表中的字段。

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

你不需要分組。 我認為假設InspectionIDInspection的主鍵是安全的,因此一個組總是包含一個Inspection ,這使得分組毫無意義。 您可以通過以下方式相對簡單地查詢您想要的內容:

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

投向DateTime? 是在沒有任何tblInspectionDate的情況下考慮檢查。

旁注:從類和屬性名稱中刪除這些累人的tbl前綴,並為集合使用復數名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM