说我有的数据结构的IEnumerable<&IEnumerable的LT;对象>> 是这样的:
{ { A, B } { 1, 2, 3 } { Z } }凡外数组可以包含的内阵列的任何数量的的。和内部数组可以各自独立地包含的任意数量的的元素。并假定,为了简单起见,没有阵列将是空的。
Where the outer array can contain any number of inner arrays. And the inner arrays can each independently contain any number of elements. And assume, for the sake of simplicity, that no array will be empty.
和我想将其转换为一个的IEnumerable<&IEnumerable的LT;对象>> 是这样的:
And I want to transform it to a IEnumerable<IEnumerable<object>> like this:
{ { A, 1, Z }, { A, 2, Z }, { A, 3, Z }, { B, 1, Z }, { B, 2, Z }, { B, 3, Z } }其中包含从原始结构中的值的每一个组合。所以每个内阵列中的每个元素由索引映射到原始外数组中的元素/阵列
Which contains every combination of the values from the original structure. So each element in each inner array maps by index to an element/array in the original outer array.
什么是做,在C#中最简单的方法是什么?
What is the simplest way to do that in C#?
推荐答案您可以为此使用笛卡儿积方法,通过埃里克利珀(从here):
You could use CartesianProduct method by Eric Lippert for this (taken from here):
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})); }更多推荐
创建N * M值的所有组合
发布评论