Posted in:

The fourth video in my Exploring MoreLINQ series looks at the Permutations, Subset and Cartesian methods, which are all related to calculating combinations of items.

Permutations enumerates all possible orderings of the items in the input sequence. Subset gives you every possible subset of the input sequence, including the empty subset, and the subset of all items. This is also sometimes known as a "powerset", and you can ask for only subsets of a certain size. Finally there's Cartesian allowing you to find all the combinations of picking one item from each of two or more sequences.

Some "real-world" examples of when these methods come in handy are when I needed to work out what containers to use to hold 150 litres of eggnog which required powersets, or solving the travelling salesman problem with Permutations.

You can find all videos in this series here.

Want to learn more about LINQ? Be sure to check out my Pluralsight course LINQ Best Practices.

Comments

Comment by Seugruom

Hello Mark,
I have a question for you, do you know how many permutations can generate the "Permutations()" method ?
I'm trying to use this method but I noticed that if my source List has more than 15 items (approximately) in it, the Permutations() method just generates one IList which is just a copy of my source List.
I hope you will see this message, I'am looking for an answer since 2 hours ...
I wish you a wonderful day.
Thanks

Seugruom