2

I've to figure out how to generate SKUs for a product with a certain number (x) of attributes and each attribute has a certain number (y) of values. For example:

A t-shirt has the following attributes:

  • Color
  • Size
  • Gender
  • Sleeve

And the values for those attributes are:

  • Color: Red, White, Blue
  • Size: S, M, L, XL
  • Gender: M, F
  • Sleeve: Short, Long

I need to create a SKU for each unique combination e.g. SKU #: 1234 for Small, Red, Male w/ short sleeves, SKU #: 2345 for Small, Red, Male, Long Sleeve.

My Class:

public class Sku
{
    public int Id { get; set; }

    public int ProductId { get; set; }

    public string Name { get; set; }

    public List<SkuDetail> SkuDetails { get; set; };
}


public class SkuDetail
{
    public int Id { get; set; }

    public int SkuId { get; set; }

    public string Name { get; set; }
}

Demo data:

var skus = new List<Sku>();
skus.Add(new Sku
{
    Name = "Size",
    SkuDetails = new List<SkuDetail>
    {
        new SkuDetail {Name = "S"}, new SkuDetail {Name = "M"},
        new SkuDetail {Name = "L"}, new SkuDetail {Name = "XL"}, new SkuDetail {Name = "XXL"}
    }
});
skus.Add(new Sku
{
    Name = "Color",
    SkuDetails = new List<SkuDetail>
    {
        new SkuDetail {Name = "Red"}, new SkuDetail {Name = "White"},
        new SkuDetail {Name = "Black"}
    }
});
skus.Add(new Sku
{
    Name = "Style",
    SkuDetails = new List<SkuDetail>
        {new SkuDetail {Name = "Modern"}, new SkuDetail {Name = "classic"}}
});

I'd like to use the most efficient approach but I can only think of nested foreach statements for this. I could use some help with the logic here.

RBT
  • 24,161
  • 21
  • 159
  • 240
TuanNV
  • 21
  • 1
  • 3

0 Answers0