Every 2 Letter Combo for Array of Strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sloan

    Every 2 Letter Combo for Array of Strings


    string[] array = new string[3] {"A","B","C" };


    Given the above data, I'm trying to find C# code which will produce




    A,A
    B,A
    C,A
    A,B
    B,B
    C,B
    A,C
    B,C
    C,C


    Its been a long week, and I'm brain dead, and googled out.


    Thanks for any help.




  • Marc Gravell

    #2
    Re: Every 2 Letter Combo for Array of Strings

    Erm, loop?

    string[] array = { "A", "B", "C" };
    for(int i = 0; i < array.Length; i++)
    for (int j = 0; j < array.Length; j++)
    {
    Console.WriteLi ne("{0},{1}", array[j], array[i]);
    }

    You could do something more exciting, but that should do...

    Marc

    Comment

    • sloan

      #3
      Re: Every 2 Letter Combo for Array of Strings

      Like I said dude....brain dead.


      "Marc Gravell" <marc.gravell@g mail.comwrote in message
      news:7981a025-7a58-43f1-b260-5f457fd5f10a@v4 g2000hsf.google groups.com...
      Erm, loop?
      >
      string[] array = { "A", "B", "C" };
      for(int i = 0; i < array.Length; i++)
      for (int j = 0; j < array.Length; j++)
      {
      Console.WriteLi ne("{0},{1}", array[j], array[i]);
      }
      >
      You could do something more exciting, but that should do...
      >
      Marc

      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Every 2 Letter Combo for Array of Strings

        Something more exciting being (if the OP is using .NET 3.5/C# 3.0):

        string[] array = { "A", "B", "C" };

        IEnumerable<str ingquery =
        from a in array
        from b in array
        select a + "," + b // Could be string.Format(" {0},{1}", a, b) as well if
        you wish.

        foreach (string item in query)
        {
        Console.WriteLi ne(item);
        }

        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m


        "Marc Gravell" <marc.gravell@g mail.comwrote in message
        news:7981a025-7a58-43f1-b260-5f457fd5f10a@v4 g2000hsf.google groups.com...
        Erm, loop?
        >
        string[] array = { "A", "B", "C" };
        for(int i = 0; i < array.Length; i++)
        for (int j = 0; j < array.Length; j++)
        {
        Console.WriteLi ne("{0},{1}", array[j], array[i]);
        }
        >
        You could do something more exciting, but that should do...
        >
        Marc

        Comment

        • Marc Gravell

          #5
          Re: Every 2 Letter Combo for Array of Strings

          Like I said dude....brain dead.

          I know that feeling;-p

          Currently in "crunch"; I don't expect to be getting to sleep any time
          soon...

          Marc

          Comment

          • Marc Gravell

            #6
            Re: Every 2 Letter Combo for Array of Strings

            (laughs)

            I actually deliberately stopped myself posting the LINQ version, aware
            (over-cautious) that LINQ isn't yet fully mainstream... oh well ;-p

            Marc

            Comment

            • Marc Gravell

              #7
              Re: Every 2 Letter Combo for Array of Strings

              IEnumerable<str ingquery =
                  ...
                  select new { A1Label = a, A2Label = b, NotSame = (a != b) };
              (minor; just for the OP really) - you'd need "var query" here because
              of the anonymous type in the projection

              Marc

              Comment

              • Jeroen Mostert

                #8
                Re: Every 2 Letter Combo for Array of Strings

                Nicholas Paldino [.NET/C# MVP] wrote:
                Well, in SQL, you don't have to explicitly state CROSS JOIN anymore (and
                I do believe it is actually not recommended now, but I'd have to look that
                up), you can just do:
                >
                select
                a1.AlphabetLabe l as A1Label, a2.AlphabetLabe l as A2Label,
                case a1.AlphabetLabe l when a2.AlphabetLabe l then 0 else 1 end as NotSame
                from
                Alphabet as a1, Alphabet as a2
                >
                CROSS JOIN was introduced in SQL-92 to solve some ambiguity problems with
                the comma notation and increase clarity (before this you couldn't use them,
                so it's not a case of "not having to state it anymore"; the comma notation
                came first). I'm not aware of any explicit recommendation for or against,
                and either syntax will work fine.

                However, it's still a very good idea to write out the CROSS JOIN explicitly,
                since it's the most uncommon form of join (even if it's the most natural
                one, mathematically speaking) and alerting your fellow developer to its use
                is a good idea. An "accidental " cross join is not a good thing to have (as
                even moderately-sized tables will produce enormous result sets) which is
                exactly why ANSI SQL introduced explicit syntax for them.

                --
                J.

                Comment

                • Marc Gravell

                  #9
                  Re: Every 2 Letter Combo for Array of Strings

                  that'd better not be a ghost-busters joke!

                  Comment

                  Working...