Calculer toutes les paires possibles d'éléments de deux listes?
-
19-09-2019 - |
Question
J'ai deux tableaux:
string[] Group = { "A", null, "B", null, "C", null };
string[] combination = { "C#", "Java", null, "C++", null };
Je veux retourner toutes les combinaisons possibles comme:
{ {"A","C#"} , {"A","Java"} , {"A","C++"},{"B","C#"},............ }
NULL doit être ignorée.
La solution
Group.Where(x => x != null)
.SelectMany(g => combination.Where(c => c != null)
.Select(c => new {Group = g, Combination = c}));
Vous pouvez également:
from g in Group where g != null
from c in combination where c != null
select new { Group = g, Combination = c }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow