问题
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
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.
回答1:
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();
来源:https://stackoverflow.com/questions/47784864/cartesian-product-of-string-with-itself-without-duplicate-clone-directly-in-c