I have a table of Products and a table of Product_Images with a one to many relation (one product can have multiple images) and I am trying to retrieve all products with their related images. So far I have:
var results = (from p in _context.Products
join i in _context.Product_Images on p.Id equals i.Product.Id
select new
{
p,
i
}).ToList();
However that returns separate results for each image, even if they are related to the same product:
{product1, product1Img1}
{product1, product1Img2}
{product1, product1Img3}
I need the results to contain the product and a list of all its related images like so:
{product1, [product1Img1, product1Img2, product1Img3]}
{product2, [product2Img1, product2Img2, product2Img3]}
{product3, [product3Img1, product3Img2, product3Img3]}
I have tried using group by on the images after joining the tables but get either a “The method ‘GroupBy’ is not supported.” or “The ‘GroupJoin’ operation must be followed by a ‘SelectMany’ operation where the collection selector is invoking the ‘DefaultIfEmpty’ method.” exception, and I can only assume my syntax is incorrect.