Calcule todos os pares possíveis de itens de duas listas?
-
19-09-2019 - |
Pergunta
Eu tenho duas matrizes:
string[] Group = { "A", null, "B", null, "C", null };
string[] combination = { "C#", "Java", null, "C++", null };
Gostaria de voltar todas as combinações possíveis, como:
{ {"A","C#"} , {"A","Java"} , {"A","C++"},{"B","C#"},............ }
O nulo deve ser ignorado.
Solução
Group.Where(x => x != null)
.SelectMany(g => combination.Where(c => c != null)
.Select(c => new {Group = g, Combination = c}));
Como alternativa:
from g in Group where g != null
from c in combination where c != null
select new { Group = g, Combination = c }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow