字符串[]的笛卡尔积,其本身没有重复/克隆,直接在C#中 [英] Cartesian product of string[] with itself without duplicate/clone directly in C#

查看:90
本文介绍了字符串[]的笛卡尔积,其本身没有重复/克隆,直接在C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅我缺乏更好的术语。我有一个 string [] ,我想获得一个与自己连接的乘积连接的乘积,以便稍后放入字典中。但是,如果有一个克隆/重复键,我希望所有可能的组合除了

Please excuse my lack of better terminology. I have a string[] and I'd like to get the product of a catesian join with itself for later to put in a dictionary. However, I'd like all possible combinations except when there is a clone/duplicate key.

string[] array1 = { "aa", "bb", "cc" };
string[][] arrayOfArrays = array1.SelectMany(left => array1, (left, right) => new string[] { left, right }).ToArray();

将获得字符串[] [] 从所有可能的组合产生的每个元素:

Will obtain a string[][] from all possible combinations yielding as each element:

aa aa
aa bb
aa cc
bb aa
bb bb
bb cc
cc aa
cc bb
cc cc

我想在这里理解的是当您得到如下结果时调用的联接:

What I'm trying to understand here is what is the joining called when you have the result like this:

aa bb
aa cc
bb aa
bb cc
cc aa
cc bb

这样,当我制作字典时,我不必进入for循环并取出值对的那些键。如果您在1-liner或lambda中对此进行了演示,那么我很乐意看到它,因为我试图进一步了解这种语法。

That way when I make my dictionary I don't have to go in with a for loop and take out those keys to the value pair. If you have a demonstration of this in a 1-liner or lambda I'd love to see it as I'm trying to understand more about this kind of syntax.

推荐答案

我不确定它的名称,但是您可以使用 Where 子句过滤掉那些匹配的值。

I'm not sure it has a name, but you can use a Where clause to filter out those matching values.

string[][] arrayOfArrays =
    array1.SelectMany(left => array1, (left, right) => new string[] { left, right })
          .Where(x => x[0] != x[1])
          .ToArray();

这篇关于字符串[]的笛卡尔积,其本身没有重复/克隆,直接在C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆