use*_*672 2 .net c# arrays list
我有一系列的列表
List<string>[] possibleLines;
Run Code Online (Sandbox Code Playgroud)
数组可以具有不同的大小,每个List <>可以具有不同数量的字符串.例如
List<string>[0] - 可以有字符串"first string","second string"List<string>[1] - "第三弦","第四弦","第五弦"我需要获得所有可能的组合,每个字符串必须来自不同的列表(数组大小可能不同).例如
等等.
你在这里做的是计算未知数量的集合的笛卡尔积.Eric Lippert描述了如何在这篇博文中写出这个问题的解决方案(我强烈建议你阅读,看看他是如何提出这个解决方案的).
他最终得到的代码是:
static IEnumerable<IEnumerable<T>> CartesianProduct<T>(
this IEnumerable<IEnumerable<T>> sequences)
{
IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() };
return sequences.Aggregate(
emptyProduct,
(accumulator, sequence) =>
from accseq in accumulator
from item in sequence
select accseq.Concat(new[] {item}));
}
Run Code Online (Sandbox Code Playgroud)