Im trying to figure out a nice way to populate a nested collection model. I have the following in my viewmodel.
public class ListViewModel
{
public ICollection<Wish> Wishes { get; set; }
}
The Wish model looks like this:
public class Wish
{
public ICollection<Image> Images { get; set; }
}
Now in my controller I want to populate the ListViewModel with wishes also populate each wish with their corresponding images. What I have so far:
public IActionResult Index()
{
ICollection wishes = _repoWish.GetAllAsync().Result;
ICollection images = _repoImage.GetAllAsync().Result;
var model = new ListViewModel
{
Wishes = wishes
};
return View(model);
}
I know I can make a lot of foreach statements but I want to make use of LINQ to populate each wish with their corresponding images.
**I do have a generic repository class which makes it possible for me to retrieve all images in the same manner as the wishes.
*** Think about the repositories as contexts. So instead of _repoWish and _repoImage its wishContext and imageContext
I am using ASP.NET Core 2.0 with Entity Framework Core