NULL 数组上的笛卡尔坐标
我需要 6 个数组的笛卡尔积 - 问题是任何时候最多 5 个数组可能为空。当所有数组都填充时它工作得很好,但当任何数组为空时它会爆炸
我的数组是这样的
MatrixArray_1[0] = 1
MatrixArray_1[1] = 2
MatrixArray_2[0] = null
MatrixArray_2[1] = null
MatrixArray_n[0] = 2
MatrixArray_n[1] = 2
等等。
我当前正在使用此代码...源自 http:// /blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx
var product1 = from first in MatrixArray_1
from second in MatrixArray_2
from third in MatrixArray_3
from fourth in MatrixArray_4
from fifth in MatrixArray_5
from sixth in MatrixArray_6
select new[] { first, second, third, fourth, fifth, sixth };
string[][] myCombos_linq = product1.ToArray();
我已尝试将 MatrixArray_n 放置在第一个 != null 位置,但会在第一个空数组处停止,并且不会读取所有剩余数组,因此即使填充了 array1 和数组 3,我的返回数组始终为 0 行。
此时,代码/逻辑的任何更改都值得赞赏! TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于 Eric 的方法是使用
IEnumerable>
,因此您必须执行以下操作:Eric 的代码:
调用站点:
更改调用站点:
Where 调用将排除所有序列元素为空。
要排除空数组以及仅包含空值的数组:
或者,使用查询理解:
编辑
另一种可能性是您想要排除每个序列的空元素,即使某些元素是非空的:
或者
但是很难确切地知道应该提供什么建议,因为我们不知道您正在对结果做什么。
Since Eric's approach is to use an
IEnumerable<IEnumerable<T>>
, you must be doing something like this:Eric's code:
Call site:
Change the call site:
The Where call will exclude sequences where all elements are null.
To exclude null arrays as well as arrays containing only null values:
Or, with query comprehension:
EDIT
Another possibility is that you want to exclude null elements of each sequence, even if some elements are non-null:
OR
But it's hard to know exactly what to advise since we don't know what you're doing with the result.
我是否正确理解您想要排除
first
、second
等中的任何一个(如果它们为null
)?这很简单:只需添加
到您的查询中即可。
或者如果
first
、second
等中的任何一个为null
,您是否想要排除整个六元组?这也很容易。只需添加到您的查询中即可。您甚至可以使用
let
这样就不会创建数组两次。Am I understanding correctly that you want to exclude any of
first
,second
, etc. if they arenull
? That's easy:Just add
to your query.
Or is that you want to exclude the entire sextuple if any of
first
,second
, etc. arenull
? That's easy too. Just addto your query. You can even use
let
so you're not creating the array twice.根据定义(我引用前面提到的 Eric Lippert
如果概括这个定义,它将是:
任何 n 个序列 S1、S2、...Sn 的笛卡尔积是所有可能的 n 元素序列的序列其中第一个元素来自 S1 并且第二个元素来自 S2,N 元素来自 Sn
请注意,第一个元素应始终来自第一个元素。
如果您过滤掉第一个数组的空值,这意味着整个输入将丢失,则过滤出的输入如下所示:
这是一个完整的示例:
,让我们应用 First != null 的过滤器。
假设我们将 array1 的值更改为:
结果为空集。为了获得笛卡尔积,您必须从每个数组中获得一个输入。您的问题可以调整以解决这一事实。
By definition (and I am quoting from the previously mentioned Eric Lippert blog).
"The Cartesian product of two sequences S1 and S2 is the sequence of all possible two-element sequences where the first element is from S1 and the second element is from S2."
If you generalize the definition it would be:
The Cartesian product of any n sequences S1, S2,...Sn is the sequence of all possible n-element sequences where the first element is from S1 and the second element is from S2 and the N element is from Sn
Note that the first element should ALWAYS be from the first element.
If you are filtering out null values of the first array that means that the whole entery would be missing, the filtered out enterys looks like this:
Here is a complete example:
Now, let's apply the filter where First != null.
Say that we changed the value of array1 to:
The result is empty set. In order to have a cartesian product, you have to have an entery from each array. Your question could be adjusted to address this fact.