Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate all possible pairs of items from two lists?

I have two arrays:

string[] Group = { "A", null, "B", null, "C", null };

string[] combination = { "C#", "Java", null, "C++", null }; 

I wish to return all possible combinations like:

{ {"A","C#"} , {"A","Java"} , {"A","C++"},{"B","C#"},............ }

The null should be ignored.

like image 707
user160677 Avatar asked Aug 25 '09 12:08

user160677


People also ask

How do you make all combinations with two lists?

Once the queries from the tables are ready, go to Data > Get Data > Combine Queries > Merge to open the Merge dialog of Power Query. Select each table in the drop downs. Click on the column for each table to select them. Finally select Full Outer option for Join Kind to join by all rows from both tables.

How do you generate all possible combinations of items from multiple lists Excel?

Go to the Data tab and select Get Data from the Get & Transform Data section. Select Combine Queries from the menu and then select Merge from the submenu.

How do you find all possible combinations?

To calculate combinations, we will use the formula nCr = n! / r! * (n - r)!, where n represents the total number of items, and r represents the number of items being chosen at a time. To calculate a combination, you will need to calculate a factorial.


1 Answers

Group.Where(x => x != null)
     .SelectMany(g => combination.Where(c => c != null)
                                 .Select(c => new {Group = g, Combination = c})
     );

Alternatively:

from g in Group where g != null
from c in combination where c != null
select new { Group = g, Combination = c }
like image 57
mmx Avatar answered Sep 24 '22 01:09

mmx