Manny
Manny

Reputation: 41

dynamic nested looping

I have a list of objects dynamically created. MyObject is a custom object consisting of List of Items. So for example :

MyObject Instance1 has { a, b }

MyObject Instance2 has { x, y, z}

MyObject Instance3 has { 1, 2, 4, 5 }

...............

..............

MyObject InstanceN has { n1, n2 ............}

I want to be able to use either recursion or an iterative approach to solve the problem of generating all unique combinations.

How do I generate all unique combinations .. an output like this

{a, x, 1,......., n1}
{a, x, 1,......., n2}
{a, x, 2,......., n1}
{a, x, 2,......., n2}

Upvotes: 4

Views: 1680

Answers (1)

LBushkin
LBushkin

Reputation: 131746

What you're looking for is the Cartesian Product, and Eric Lippert has written an excellent blog post, with sample code for how to do this in LINQ, that you can probably adapt.

You can also look at this question, which is similarly related.

Upvotes: 4

Related Questions