user160677
user160677

Reputation: 4303

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.

Upvotes: 20

Views: 17780

Answers (1)

Mehrdad Afshari
Mehrdad Afshari

Reputation: 422172

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 }

Upvotes: 54

Related Questions