John Skeets answer to this question, Select all unique combinations of a single list, with no repeats, using LINQ, works awesomely.
However, can someone break down component by component the inner workings of how the first answer is working:
List<int> slotIds = new List<int> {1, 2, 3};
var query = slotIds.SelectMany((value, index) => slotIds.Skip(index + 1),
(first, second) => new { first, second });
It’s roughly equivalent in concept to this, although the actual execution model is different of course (lazy etc):
The
SelectManytaking a projection fromvalueandindexis a way of using bothfirstandito make an inner loop. We need the index so that we can skipindex + 1values, which is equivalent to thejloop starting ati + 1in the above code.Does that help? If not, could you pinpoint which bit is confusing?
EDIT: Aargh – I hadn’t realized that the other question you were referring to started out with this code! I think it’s still useful though, to give the paragraph below something to hang on…
If you understood the alternative (query expression) version of my answer, then the first version is similar, just using an overload of
SelectManywhich allows you to use both the value and index in the “outer” sequence.