Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's a Cartesian product of Cartesian products:</p> <pre><code>var groups = from x in (from v in values from s in states select new {v,s}) group x by x.v into gx select gx; var perms = from a in groups[0] from b in groups[1] from c in groups[2] select new {a,b,c}; </code></pre> <p>The groups query produces a Lookup (conceptually a read-only Dictionary of IEnumerables) containing the simple Cartesian product of all values and states (6 elements), grouped by their value. Then, the second query produces a Cartesian product of the elements of the Cartesian product taken three at a time, one from each group in the Lookup. </p> <p>To make this work with an unknown number of dimensions would be tricky; if you don't absolutely have to make it work that way I would avoid it. I think the most elegant way would be to define a set of extension methods for the System.Tuple generic classes:</p> <pre><code>public static Tuple&lt;T1,T2&gt; Append(this Tuple&lt;T1&gt; tuple, T2 addend) { return Tuple.Create(tuple.Item1, addend); } public static Tuple&lt;T1,T2, T3&gt; Append(this Tuple&lt;T1,T2&gt; tuple, T3 addend) { return Tuple.Create(tuple.Item1, tuple.Item2, addend); } ... </code></pre> <p>Then, you can take these helpers and use them in a looped version of the second query:</p> <p>var perms = from a in groups[0] select Tuple.Create(a);</p> <p>foreach(var group in groups.Skip(1)) perms = from a in perms from b in group select a.Append(b);</p> <p>This will produce an enumerable of Tuples of the required length, containing the elements of the anonymous type produced in the first query (which can be refactored to produce strongly-typed 2-item Tuples if you wish). You may have an issue with using the perms collection variable to refer to collections of ever-growing Tuples; this is the tricky part.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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