1

class demo:

class item
{
    public string name { get; set; }
    public int level { get; set; }
}

data demo:

List<item> all = new List<item>();
all.Add(new item { name = "Red", level = 0 });
all.Add(new item { name = "Blue", level = 0 });

all.Add(new item { name = "S", level = 1 });
all.Add(new item { name = "M", level = 1 });
all.Add(new item { name = "L", level = 1 });

all.Add(new item { name = "Man", level = 2 });
all.Add(new item { name = "Woman", level = 2 });

I need group by level and combine all name , This is Cartesian Product question. The result like this:

Red - S - Man
Red - S - Woman
Red - M - Man
Red - M - Woman
Red - L - Man
Red - L - Woman
Blue - S - Man
Blue - S - Woman
Blue - M - Man
Blue - M - Woman
Blue - L - Man
Blue - L - Woman

If the level was fixed, solution with bellow code :

foreach(var _0 in all.Where(m => m.level == 0))
{
    foreach(var _1 in all.Where(m => m.level == 1))
    {
        foreach(var _2 in all.Where(m => m.level == 2))
        {
            Console.WriteLine(_0.name + "-" + _1.name + "-" + _2.name);
        }
    }
}

But the big question : level is dynamic , I just coding like this :

for(int i = 0; i < level; i++)
{
    //some code ...
}

Because my real project is Javascript , so please give me simple code (non linq), Thanks very much to help .

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
jian.wu
  • 85
  • 3

2 Answers2

1

I would start with building a list containing items per each level:

var levels = new List<List<item>>();
foreach (var item in all)
{
    while (levels.Count <= item.level)
        levels.Add(new List<item>());
    levels[item.level].Add(item);
}

and then populate the result using a simple recursive method:

var result = new List<string>();
AddCombinations(result, levels, 0, null);

where the method is:

static void AddCombinations(List<string> result, List<List<item>> levels, int level, string path)
{
    if (level >= levels.Count)
    {
        result.Add(path);
        return;
    }
    foreach (var item in levels[level])
        AddCombinations(result, levels, level + 1, path == null ? item.name : path + " - " + item.name);
}

Instead of recursion, I could adjust the implementation from my answer to Every combination of "1 item from each of N collections" to build the result iteratively in place if you wish, but I think the above should be sufficient.

Community
  • 1
  • 1
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
0

Something like this should work:

var lines = CartesianProduct(all, 0);
foreach(var line in lines) {
   Console.WriteLine(line);
}

List<string> CartesianProduct(List<item> items, int level) {
   List<string> result = new List<string>();
   List<string> itemsOnThisLevel = new List<string>();
   foreach(var it in items) {
      if (it.level == level) itemsOnThisLevel.Add(it.name);
   }
   if (!itemsOnThisLevel.Any()) {
      result.Add("");
      return result;
   }
   var itemsOnLowerLevels = CartesianProduct(items, level+1);
   foreach(var it in itemsOnThisLevel) {
      foreach(var it2 in itemsOnLowerLevels) {
         result.Add(it2 + " - " + it);
      } 
   }
   return result
}

EDIT: removed linq expressions as the author requested.

suwik
  • 110
  • 7