This is my models
public class Product
{
[Key]
public int SerialNumber { get; set; }
public int PartNumber { get; set; }
public string Description { get; set; }
public virtual ICollection<Reading> Reading { get; set; }
}
public class Reading
{
[Key]
public int Id { get; set; }
public int SerialNumber { get; set; }
public int ReadingValue { get; set; }
public virtual Product Product { get; set; }
}
I can send all products to the view with
return View(db.Products.ToList().Where(product => product.CustomerID == Customer));
And I can get the latest ReadingValue if I know the Product SerialNumber
var LatestReading = db.Readings.OrderByDescending(m => m.Id).Where(s => s.SerialNumber == SerialNumber).Select(m => m.ReadingValue).FirstOrDefault();
How can I send all the products to the view with the latest ReadingValue for each product?
Create a new view model that will hold both the data:
Change your view to use the new model with:
Then send them back in your controller: