I have a Collection (List) of items (String). Number of items in this collection will always be between 0 to 9.
I need to create all combinations of pairs and triples from this collection.
Position of item in double or triplet does not matter. So {1,2} is equal to {2,1}.
How can i achieve this? Maybe there is some nice way to do this via LINQ?
In the code below I generate all unique doubles and triplets using linq. I use the fact that strings have a total ordering.
This generates all doubles:
This generates all triplets:
Update: There is a generic solution to create all unique subsets of a specific length, and still use linq. However, you need a returntype that can contain the subset. I created a simple class
LinkedNode, because to me this feels most natural in combination with linq:It should be easy to implement
IEnumerable<string>on the classLinkedNode, or otherwise convert the LinkedNodes to aList<string>orHashSet<string>. Note that you can remove the lineorderby a, b.Valueif the order is not important.