简体   繁体   中英

EF Core make group join list to object

I am trying to join a list into my object via linq. Following my linq query

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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