-4

How I can get ordered pairs of elements from some array, using LINQ? For example, I have:

int[] d = { 1, 2, 3 };

I need:

{ {1, 1}, {1, 2}, ...., {3, 3} }

I tried that LINQ query, but it returns

{ {1, 1}, {2, 2}, {3, 3}, {1, 1}, {2, 2}, {3, 3}, {1, 1}, {2, 2}, {3, 3} }

var pairs = d.SelectMany(a => d.Select(b => new[] { a, b }));

Please, help me to find my error.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • 6
    The code you've provided yields the sequence you claim to want, not the sequence you're saying it does. – Servy Oct 14 '16 at 21:09
  • 1
    Are you looking for https://en.wikipedia.org/wiki/Cartesian_product (which covered already in many questions including http://stackoverflow.com/questions/3093622/generating-all-possible-combinations )? As @Servy code in the post seem to produce result you want... – Alexei Levenkov Oct 14 '16 at 21:18

2 Answers2

1

Like this:

var result = d.SelectMany(a => d, Tuple.Create)
              .Where(c=> c.Item1 <= c.Item2).ToArray();
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • 1
    That is beautiful – Matias Cicero Oct 14 '16 at 21:12
  • @MatiasCicero But the OP didn't state that he wants pairs where the first item is less than the second, so this produces the wrong output. – Servy Oct 14 '16 at 21:14
  • @Servy If the OP's code works as he wanted why he should ask a question? Based on his post I think he is looking for a combination that the first item is less than the second or equal. – Salah Akbari Oct 14 '16 at 21:18
  • @S.Akbari I don't know why he asked the question given that he had the code that generates the code that he wanted, that's why I asked him. But regardless, he *didn't* ask for this. This answer is incorrect. – Servy Oct 14 '16 at 21:21
  • @Servy Compare the OP's desired result and his gotten result. The intersect of both is(the first item is less than the second or equal) But in his desired result I can't see even one item that the second item is less than the first. This was my presumption. BTW I'm waiting for OP's feedback. :) – Salah Akbari Oct 14 '16 at 21:26
  • @S.Akbari The OP was rather specific about his requirements. He didn't ask for literally any code that produces a sequence that contains at least the 3 values shown in the output. If he wanted to exclude values where the first is greater than the second, he would have *stated as much in his requirements*. Inventing requirements that completely contradict the stated requirements, just because the one, incomplete, example happens to be consistent with your alternate requirements doesn't make your answer correct. – Servy Oct 14 '16 at 21:29
-1

this code work

        int[] d = { 1, 2, 3 };

        var query = (from elem1 in d
                     from elem2 in d 
                     where elem1>= elem2
                     select elem1< elem2? new List<int>() { elem1, elem2 }: new List<int>() { elem2, elem1 }
                     ).Distinct().ToArray();
Esperento57
  • 16,521
  • 3
  • 39
  • 45