Question

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.

Was it helpful?

Solution

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 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top