I'm building an app in .net core 3.1. I want to show a list of Assets along with the last date each asset had a visual and a full pat test.
The tables/models I'm using are as follows:
public class Asset
{
public int AssetID { get; set; }
public string Title { get; set; }
public int AssetCatID { get; set; }
public string Manufacturer { get; set; }
public bool RequiresPAT { get; set; }
public AssetCat AssetCat { get; set; }
public PAT PAT { get; set; }
}
public class AssetCat
{
public int AssetCatID { get; set; }
public string CategoryTitle { get; set; }
public virtual ICollection<Asset> Assets { get; set; }
}
public class PAT
{
public int PATID { get; set; }
public int AssetID { get; set; }
public int CheckTypeID { get; set; }
public DateTime CheckDate { get; set; }
public string CheckedBy { get; set; }
public Asset Asset { get; set; }
public CheckType CheckType { get; set; }
}
I haven't included the CheckType table here, but CheckTypeID 1 = Full Test, CheckTypeID 2 = Visal Test
Each Asset that RequiresPAT will have multiple entries for both types of test, and I need to list the assets along with the date of the last test of each type.
var AssetQuery = _context.Assets
.Where(a => a.RequiresPAT)
.Include(a => a.AssetCat)
.Include(a => a.PAT) // needs to just include latest PAT record where CheckTypeID = 1 if it exists AS FullPAT
.Include(a => a.PAT) // needs to just include latest PAT record where CheckTypeID = 2 if it exists AS VisualCheck
.ToListAsync();
I've seen that using a .Where on a include is coming later in EF 5, but isn't available yet. I've tried using varations of .OrderByDecending(p => p.CheckDate).FirstorDefault() on the PAT records, but I just can't work out how to fit it all together.
I want to return a model to use on an index style page, ideally showing something like:
Model.Asset.AssetID | Model.Asset.AssetTitle | Model.Asset.AssetCat.CategoryTitle | Model.Asset.FullPat.CheckDate | Model.Asset.VisualCheck.CheckDate
I'm quite new to .net / ef / c# so any pointers would be appreciated. I know exactly how to do this writing views in SQL for classic ASP, but trying to work out the most optimal methods for .net core