Note that there are some explanatory texts on larger screens.

plurals
  1. POGenerate All Unique Combinations of Elements of a IEnumerable(Of T)
    primarykey
    data
    text
    <p>This question is virtually the same as <a href="https://stackoverflow.com/questions/1471558/all-possible-combinations-of-elements">this SO post</a>, only I'm looking for a VB.NET (.NET 4) solution. I've spun my wheels long enough trying to come up with a generic solution to solving this "power set" problem.</p> <p>Given:</p> <pre><code>Dim choices As IEnumerable(Of String) = {"Coffee", "Tea", "Milk", "Cookies"} Dim choiceSets = choices.CombineAll() </code></pre> <p>I'm looking for <code>choiceSets</code> to be an <code>IEnumerable(Of IEnumerable(Of T))</code> so that I can do something like:</p> <pre><code>For each choiceSet in choiceSets Console.WriteLine(String.Join(", ", choiceSet)) Next </code></pre> <p>And get results that look like:</p> <pre><code>Coffee Tea Milk Cookies Coffee, Tea Coffee, Milk Coffee, Cookies Tea, Milk Tea, Cookies Milk, Cookies Coffee, Tea, Milk Coffee, Tea, Cookies Coffee, Milk, Cookies Tea, Milk, Cookies Coffee, Tea, Milk, Cookies </code></pre> <p>As you can see, this is every <em>non-repeating</em> combination from the source <code>IEnumerable(Of T)</code> (which could have 1 to many items in it - this example only had 4), it operates based on the order of the items in the source <code>IEnumerable(Of T)</code>, and each item in the list is >= the previous item in terms of number of items in the inner <code>IEnumerable(Of T)</code>.</p> <p>For what it's worth, this is not homework; though it sure does feel like it.</p> <p>EDIT: Updated the example so it does not look like the result is alphabetically sorted, to stress that the source <code>IEnumerable(Of T)</code>'s existing order is used and added a 4th choice to clarify the sorting requirement within each set.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload