Click here to Skip to main content
14,210,374 members
Rate this:
Please Sign up or sign in to vote.
See more:
I need to populate a Product object which contains two collections.
The current code works fine and populates the Product.GraphicItems collection, but I also need to populate the Product.InfoItems collection, but I can't figure out the syntax for multiple collections.

Current select:

var result = await this.Context.ShopCategorys
    	.Include(cat => cat.InfoItems)
    	.Include(cat => cat.Products)
    		.ThenInclude(prd => prd.GraphicItems)
    			.ThenInclude(itm => itm.Graphic)
    				.ThenInclude(gfx => gfx.Items)
    	.SingleAsync(cat => cat.Id.Equals(id));



Product.cs:

[Table("ShopProduct")]
    public class Product : BaseShop
    {
    	public bool Active { get; set; } = true;
    	public int CategoryId { get; set; }
    	public int CultureId { get; set; } = -1;
    	public List<ProductInfo> InfoItems { get; set; } = new List<ProductInfo>();
    	public List<ProductGraphic> GraphicItems { get; set; } = new List<ProductGraphic>();
    }


ProductInfo.cs:

[Table("ShopProductInfo")]
    public class ProductInfo : BaseShop, IInfoItem
    {
    	public int? ProductId { get; set; }
    	public int CultureId { get; set; }
    	public Culture Culture { get; set; }
    	public string Name { get; set; }
    	public string Description { get; set; }
    }


What I have tried:

Different .Include and .ThenInclude variations
Posted
Updated 7-Aug-17 7:25am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Solution:

var result = await this.Context.ShopCategorys
	.Include(cat => cat.InfoItems)
	.Include(cat => cat.Products)
		.ThenInclude(prd => prd.InfoItems)
	.Include(cat => cat.Products)
		.ThenInclude(prd => prd.GraphicItems)
			.ThenInclude(itm => itm.Graphic)
				.ThenInclude(gfx => gfx.Items)
	.SingleAsync(cat => cat.Id.Equals(id));
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190618.1 | Last Updated 7 Aug 2017
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100