Try this you will get a list of products and their images.
var _products = new List<Products>()
{
new Products { ProductId=1, Name="Porsche"},
new Products { ProductId=2, Name="Toyota"},
new Products { ProductId=3, Name="GMC"},
new Products { ProductId=4, Name="Dodge"}
};
var _productImage = new List<ProductImage>()
{
new ProductImage { ImageId=1, BaseProductId=1, ImageUrl="http://dummyimage.com/180x100.png/5fa2dd/ffffff" },
new ProductImage { ImageId=2, BaseProductId=1, ImageUrl="http://dummyimage.com/167x100.png/dddddd/000000" },
new ProductImage { ImageId=3, BaseProductId=2, ImageUrl="http://dummyimage.com/215x100.png/dddddd/000000" },
new ProductImage { ImageId=2, BaseProductId=2, ImageUrl="http://dummyimage.com/215x100.png/dddddd/000000" },
new ProductImage { ImageId=2, BaseProductId=2, ImageUrl="http://dummyimage.com/215x100.png/dddddd/000000" },
new ProductImage { ImageId=4, BaseProductId=2, ImageUrl="http://dummyimage.com/215x100.png/dddddd/000000" }
};
var GroupJoin = _products.
GroupJoin(
_productImage,
pro => pro.ProductId,
img => img.BaseProductId,
(pro, img) => new { pro, img }
);
foreach (var item in GroupJoin)
{
Console.WriteLine("product :" + item.pro.Name);
foreach (var imgItem in item.img)
{
Console.WriteLine(" Image ID : " + imgItem.ImageId + " , Image URL : " + imgItem.ImageUrl);
}
Console.WriteLine();
}
Result:
product :Porsche
Image ID : 1 , Image URL : http://dummyimage.com/180x100.png/5fa2dd/ffffff
Image ID : 2 , Image URL : http://dummyimage.com/167x100.png/dddddd/000000
product :Toyota
Image ID : 3 , Image URL : http://dummyimage.com/215x100.png/dddddd/000000
Image ID : 2 , Image URL : http://dummyimage.com/215x100.png/dddddd/000000
Image ID : 2 , Image URL : http://dummyimage.com/215x100.png/dddddd/000000
Image ID : 4 , Image URL : http://dummyimage.com/215x100.png/dddddd/000000
product :GMC
product :Dodge