1

I have two lists, one for the items and the other for its values, how can i print the contents like this Example:

Items List -> [1], [2]
valuesList1 -> a, b
valuesList2 -> c, d 

Desired Output:

case 1: 1-a 2-c
case 2: 1-a 2-d
case 3: 1-b 2-c
case 4: 1-b 2-d

My code:

foreach (var item in listItem)
            {

                foreach (var itemValue in item.listItemValue)
                { 

                    text +=  item.value + " - " + itemValue.value + "\n";

                }
                 text += "\n";
            }

 MessageBox.Show(text);

Output:

1 - a
1 - b
2 - c
2 - d

EDIT

Desired output:

case 1: 1-a 2-c
case 2: 1-a 2-d
case 3: 1-b 2-c
case 4: 1-b 2-d

My code and output:

int n = 0;
var produtoCartesiano = itens.SelectMany(x => valoresItens.Select(y => x + y));

            foreach (var caso in produtoCartesiano)
            {
                result += "case " + n + ": ";
                result += caso;
                result += "\n";
                n++;
            }

MessageBox.Show(result);

Output:

case 0: 1a
case 1: 1b
case 2: 1c
case 3: 1d
case 4: 2a
case 5: 2b
case 6: 2c
case 7: 2d

What else do I need to add in my code to generate the desired output?

Marcos Jr.
  • 35
  • 1
  • 7
  • You are getting those results because you are looping through the entire second list rather than looping through the two lists pairwise. – Bradford Dillon Jan 17 '20 at 17:48
  • how can i loop them pairwise? – Marcos Jr. Jan 17 '20 at 18:08
  • 2
    @byczu While your edit did admittedly improve formatting a little, it left other formatting issues untouched and also missed at least one obvious typo. For an edit to be rewarded with reputation, it is expected to be more thorough. Please continue contributing however, you are on the right track and your effort is appreciated. Have fun. – Yunnosch Jan 17 '20 at 18:20
  • @BradfordDillon how can i loop them pairwise? – Marcos Jr. Jan 17 '20 at 18:26
  • Having different lists or arrays containing related data is an anti-pattern. Make an object that represents the related data that contains two properties. – Daniel Mann Jan 17 '20 at 18:32
  • @DanielMann this is just list of all results from [Cartesian product](https://stackoverflow.com/questions/4073713/is-there-a-good-linq-way-to-do-a-cartesian-product) - I don't see those two lists representing objects... Usually it's something like {"elf", "gnome", "troll"} x {"small", "medium", "huge"} x { "nice", "evil"} to get all possible variants of "Enemy {type, size, alignment}". – Alexei Levenkov Jan 17 '20 at 18:40

1 Answers1

0
        List<char> item1 = new List<char> { 'a', 'b' };
        List<char> item2 = new List<char> { 'c', 'd' };

        int i = 0;
        foreach (var itemab in item1)
        {
            foreach (var itemcd in item2)
            {
                i++;
                Console.Write("case {0}: ",i);
                Console.WriteLine("1-{0} 2-{1}", itemab, itemcd);
            }
        }

edit: added the output of above code:

case 1: 1-a 2-c
case 2: 1-a 2-d
case 3: 1-b 2-c
case 4: 1-b 2-d

Edit (25-01-2020):

        List<string> item1 = new List<string> { "a", "b" };
        List<string> item2 = new List<string> { "c", "d" };

        int n = 0;
        string result = "";
        var produtoCartesiano = item1.SelectMany(x => item2.Select(y => x + y));

        foreach (var caso in produtoCartesiano)
        {
            result += "case " + n + ": ";
            result += caso;
            result += "\n";
            n++;
        }

        Console.WriteLine(result);

Output:

case 0: ac
case 1: ad
case 2: bc
case 3: bd
Luuk
  • 12,245
  • 5
  • 22
  • 33
  • 1
    @AlexeiLevenkov: I do not see how my output is different from `I need to print like this:` – Luuk Jan 17 '20 at 18:28
  • Sorry, misread the question - OP asking for cartesian product and I thought … (not really sure what I actually read there). (And I should have just edited out your copyright joke instead of adding my comment inline - that was really questionable). – Alexei Levenkov Jan 17 '20 at 18:34
  • LOL, the copyright joke was indeed a joke, the original poste should try to write his own solution to the problem, and not state that his code 'does not work', and implicitly as for working code..... – Luuk Jan 17 '20 at 18:39
  • Sorry, it's not cleat to /me what `is not working as you wish`.... – Luuk Jan 22 '20 at 18:24
  • @Luuk I'm not getting the desired output, I'm now learning Cartesian Product which I think it will be the solution but I don't know what else to add in my code to generate what I need.. – Marcos Jr. Jan 23 '20 at 14:28