Possible Duplicate:
Generating all Possible Combinations
I'm not sure how to phrase the question; but I was working on a silly logic puzzle that I was able to solve using a LINQ statement. The relevant code looked like this:
(from myA in Enumerable.Range(1, 40)
from myB in Enumerable.Range(1, 40)
from myC in Enumerable.Range(1, 40)
from myD in Enumerable.Range(1, 40)
where myA + myB + myC + myD == 40
&& myA <= myB
&& myB <= myC
&& myC <= myD
select new[] {myA, myB, myC, myD})
So it's basically generating all the combinations of A,B,C D that meet the criteria in the Where clause.
What I'm trying to do now is generalize this so I can do the exact same thing with N values instead of just four. For example, with 3 values - the equivalent code would be:
(from myA in Enumerable.Range(1, 40)
from myB in Enumerable.Range(1, 40)
from myC in Enumerable.Range(1, 40)
where myA + myB + myC == 40
&& myA <= myB
&& myB <= myC
select new[] {myA, myB, myC})
Naturally, I don't want to modify the code - I want a function that I can call and provide an integer and have it return the correct object.
I've made a few misguided attempts; but I really can't see how to do something like that. Can someone point me in the right direction?