繁体   English   中英

EF Core 将组加入列表添加到 object

[英]EF Core make group join list to object

我正在尝试通过 linq 将列表加入我的 object。 按照我的 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 });

所以我的目标是将图像列表作为WebshopImages,但它只是一个object。 我如何获得清单?

试试这个,你会得到一个产品列表和它们的图像。

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();
    }

结果:

    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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM