CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Product
{
public List<Option> Options { get; set; }
public List<List<string>> GetAllCombinations()
{
if (Options == null || !Options.Any())
{
return new();
}
var combinations = new List<List<string>> { new() };
foreach (var option in Options)
{
combinations = combinations
.SelectMany(combination => option.Values
.Select(optionValue => new List<string>(combination) { optionValue }))
.ToList();
}
return combinations;
}
}
public class Option
{
public string Name { get; set; }
public List<string> Values { get; set; }
}
public static void Main()
{
Product product = new()
{
Options = new()
{
new() { Name = "Color", Values = new() { "Blue", "Yellow", "Green" } },
new() { Name = "Memory", Values = new() { " 32 GB", " 64 GB", "128 GB" } },
new() { Name = "Display", Values = new() { " HD", "FHD", "UHD" } },
}
};
var combinations = product.GetAllCombinations();
foreach (var combination in combinations)
{
Console.WriteLine(string.Join("", combination.Select(comb => comb.PadRight(10))));
}
}
}