Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can solve this kind of problems easily with LINQ.</p> <pre><code>var solutions = from pair1 in listOne where IsCandidate(pair1) from pair2 in listTwo where IsCandidate(pair1, pair2) from pair3 in listThree where IsCandidate(pair1, pair2, pair3) from pair4 in listFour where IsCandidate(pair1, pair2, pair3, pair4) from pair5 in listFive where IsCandidate(pair1, pair2, pair3, pair4, pair5) from pair6 in listSix where IsCandidate(pair1, pair2, pair3, pair4, pair5, pair6) from pair7 in listSeven where IsSolution(pair1, pair2, pair3, pair4, pair5, pair6, pair7) select new { pair1, pair2, pair3, pair4, pair5, pair6, pair7 }; </code></pre> <p>Of course this approach is valid only because the number of lists is known at compile time. An alternative approach would be to have a generic way of building the possible combinations, as <a href="http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx" rel="nofollow">Eric Lippert shows in his post</a>.</p> <p>All those intermediate <code>where</code> all over the query, are to filter out invalid combinations as soon as possible.</p> <p><strong>EDIT</strong></p> <p>Fixed solution to count efficiently how many times a same combination happens, ignoring the keys of the original source.</p> <p>To achieve this, I'm going to apply a transformation to each dictionary. I'm going to transform each dictionary into a new dictionary, where the key would be the enum value, and the value would be the number of times that enum values happens in the original dictionary.</p> <pre><code>IDictionary&lt;MyEnum, int&gt; CountOccurrences(IEnumerable&lt;MyEnum&gt; values) { return (from e in values group e by e).ToDictionary(grp =&gt; grp.Key, grp =&gt; grp.Count()); } var solutions = from p1 in CountOccurrences(listOne.Values) where IsCandidate(p1) from p2 in CountOccurrences(listTwo.Values) where IsCandidate(p1, p2) from p3 in CountOccurrences(listThree.Values) where IsCandidate(p1, p2, p3) from p4 in CountOccurrences(listFour.Values) where IsCandidate(p1, p2, p3, p4) from p5 in CountOccurrences(listFive.Values) where IsCandidate(p1, p2, p3, p4, p5) from p6 in CountOccurrences(listSix.Values) where IsCandidate(p1, p2, p3, p4, p5, p6) from p7 in CountOccurrences(listSeven.Values) where IsSolution(p1, p2, p3, p4, p5, p6, p7) select new { E1 = p1.Key, E2 = p2.Key, E3 = p3.Key, E4 = p4.Key, E5 = p5.Key, E6 = p6.Key, E7 = p7.Key, Times = p1.Value * p2.Value * p3.Value * p4.Value * p5.Value * p6.Value * p7.Value }; </code></pre>
 

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