Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create pairs from List values

Tags:

c#

.net

list

I have a list with some values, lets say 1 2 3 4 5 6

I need to pair them up like this: 12 13 14 15 16 23 24 25 26 34 35 36 45 46 56

Basically, I need to mix them all up to create unique sets of values.

Do you have any ideas on how to create a new list like this?

Thank you for your input!

like image 512
Nikita Silverstruk Avatar asked Mar 09 '26 14:03

Nikita Silverstruk


2 Answers

Using Linq and tuples:

var arr = new[] { 1, 2, 3, 4, 5, 6 };

arr.SelectMany((fst, i) => arr.Skip(i + 1).Select(snd => (fst, snd)));
like image 176
danplisetsky Avatar answered Mar 11 '26 02:03

danplisetsky


If you like Linq:

var ar = new int[] {1, 2, 3, 4, 5};

var combo = (from left in ar
            from right in ar where right > left 
            select new { left, right }).ToArray();
like image 28
Spevy Avatar answered Mar 11 '26 02:03

Spevy