[英]EF Core make group join list to object
I am trying to join a list into my object via linq.我正在尝试通过 linq 将列表加入我的 object。 Following my linq query
按照我的 linq 查询
var query = four.GroupJoin(this._context.Images,
product => product.Product.BaseProductId,
img => img.BaseProductId,
(join, images) => new
{ join.Product, images })
.SelectMany(join => join.images.DefaultIfEmpty(),
(join, img) => new EnrichedProduct
{ Product = join.Product, WebshopImages = img });
So my goal is to take the list of images as WebshopImages, but it is only a single object.所以我的目标是将图像列表作为WebshopImages,但它只是一个object。 How do I get the list?
我如何获得清单?
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.