-1
var A = new[] {A, B}; 
var B = new[] {X, Y, Z}; 
var Product = 
from _A in A 
from _B in B 
select new[] { _A, _B };
//Intent below:
foreach (pair in Product)
{
SomeFunction(pair[0],pair[1])
}

//Output:
SomeFunction(A,X)
SomeFunction(A,Y)
SomeFunction(A,Z)
SomeFunction(B,X)
SomeFunction(A,Y)
SomeFunction(A,Z)

Instead of combining the two lists into one item, how do I retrieve _A & _B as independent variables? So that for each Cartesian cross product combination (A,X..A,Y..A,Z..and so on) I can send them into another function to process?

Ideally, these variables will not be strings so any workarounds with getting character @ indicies will not work.

EDIT: Looks like I was right all along. Thank you for the community for confirming my intent.

AndyBernard
  • 105
  • 1
  • 9
  • Your code looks right to me with the description you've given. What are you seeing that isn't what you're expecting? – Greg B Dec 05 '18 at 15:38
  • You're already calling `SomeFunction` with each pair. Are you wanting to avoid the `foreach`? – Richard Dec 05 '18 at 15:38
  • Hey Guys, I suppose I just wanted to be 100% sure as I'm not able to debug at the moment and I'm not familiar enough with the Cartesian method of pairing things. So in my example by calling ele[0] I am getting the _A of the [0] item in Product and ele[1] is the _B of [0] in Product? – AndyBernard Dec 05 '18 at 15:44
  • @Richard Hey Rich any way to reduce more loops is always welcome too! – AndyBernard Dec 05 '18 at 15:44

1 Answers1

-1

I think you're 95% there, and in putting your example together you're 99% there.

Your query will give you the cartesian product as you desire.

var A = new[] {A, B}; 
var B = new[] {X, Y, Z}; 
var Product = from _A in A 
              from _B in B 
              select new[] { _A, _B };

Product is now and IEnumerable<T>. It would look like this:

[
    [A, X]
    [A, Y]
    [A, Z]
    [B, X]
    [B, Y]
    [B, Z]
]

All you need to do now is enumerate it's values, which you have in your intent (though the variable name is wrong):

//Intent below:
foreach (pair in Product)
{
    SomeFunction(pair[0], pair[1]);
}
Greg B
  • 14,597
  • 18
  • 87
  • 141
  • appreciate the help. Indeed the ele is wrong I shall update! But again it was more psdeuocode as I was typing I just wanted to confirm it was the right intent. I shall try and let you know! – AndyBernard Dec 05 '18 at 15:50