Concat two Dictionary<>

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Larsen [CPH]

    Concat two Dictionary<>

    Hi,

    How do i concat two dictionaries aka the following sample:

    Dictionary<int, stringa;
    Dictionary<int, stringb;
    b.Add(a);

    What is the easiest way to concat two dictionaries ??

    BR
    Peter


  • Paul E Collins

    #2
    Re: Concat two Dictionary&lt;& gt;

    "Peter Larsen [CPH]" <PeterLarsen@co mmunity.nospamw rote:
    How do i concat two dictionaries aka the following sample:
    Dictionary<int, stringa;
    Dictionary<int, stringb;
    What do you want to do if both dictionaries use the same integer key?
    (For example, do you want to produce an <int, string[]?)

    If you know that won't happen, then just use a loop and add each element
    of one to the other.

    Eq.


    Comment

    • Cowboy \(Gregory A. Beamer\)

      #3
      Re: Concat two Dictionary&lt;& gt;

      Are their any potential clashes in the key? In other words, suppose the
      following:

      Dictionary<int, stringa = new Dictionary<int, string>();
      Dictionary<int, stringb = new Dictionary<int, string>();

      a.Add(1,"a");
      a.Add(2,"b");

      b.Add(1, "c");
      b.Add(2, "d");

      You have a clash here. Although you can concat in one statement, in
      Framework 3.5, you will end up with a clash in keys, which is not good. If b
      is filled like so:

      b.Add(3, "c");
      b.Add(4, "d");

      you do not end up with the same issue.

      If you have clashes, you will have to loop.

      --
      Gregory A. Beamer
      MVP, MCP: +I, SE, SD, DBA

      Subscribe to my blog


      or just read it:


      *************** *************** *************** ****
      | Think outside the box!
      |
      *************** *************** *************** ****
      "Peter Larsen [CPH]" <PeterLarsen@co mmunity.nospamw rote in message
      news:%23xpGkh$k IHA.3888@TK2MSF TNGP03.phx.gbl. ..
      Hi,
      >
      How do i concat two dictionaries aka the following sample:
      >
      Dictionary<int, stringa;
      Dictionary<int, stringb;
      b.Add(a);
      >
      What is the easiest way to concat two dictionaries ??
      >
      BR
      Peter
      >

      Comment

      • Peter Larsen [CPH]

        #4
        Re: Concat two Dictionary&lt;& gt;

        I am aware about the potential danger in the sample - i'm just trying to
        figure out what linq and lambda is and what it can do for me :-)

        /Peter

        "Cowboy (Gregory A. Beamer)" <NoSpamMgbworld @comcast.netNoS pamMwrote in
        message news:ObNqIy$kIH A.980@TK2MSFTNG P06.phx.gbl...
        Are their any potential clashes in the key? In other words, suppose the
        following:
        >
        Dictionary<int, stringa = new Dictionary<int, string>();
        Dictionary<int, stringb = new Dictionary<int, string>();
        >
        a.Add(1,"a");
        a.Add(2,"b");
        >
        b.Add(1, "c");
        b.Add(2, "d");
        >
        You have a clash here. Although you can concat in one statement, in
        Framework 3.5, you will end up with a clash in keys, which is not good. If
        b is filled like so:
        >
        b.Add(3, "c");
        b.Add(4, "d");
        >
        you do not end up with the same issue.
        >
        If you have clashes, you will have to loop.
        >
        --
        Gregory A. Beamer
        MVP, MCP: +I, SE, SD, DBA
        >
        Subscribe to my blog

        >
        or just read it:

        >
        >

        Comment

        • Peter Larsen [CPH]

          #5
          Re: Concat two Dictionary&lt;& gt;

          I just want to copy the items from dictionary "a" to dictionary "b".

          Normally i would do something similar to this:
          Dictionary<int, stringb = new Dictionary<int, string>(a);

          I'm just trying to find new ways using linq...

          /Peter

          >
          >Is it possible to do something similar to the following:
          >>
          >Dictionary<int , stringa = ... {add some some items};
          >Dictionary<int , stringb = from item in a select ... what to do here
          >??
          >
          It's not entirely clear to me what you actually want to do.
          >

          Comment

          • =?Utf-8?B?SmFybGF4bGU=?=

            #6
            RE: Concat two Dictionary&lt;& gt;

            write an extension method!

            of course if you could write static extensions you could do...

            public static Concat(ref this null, Dictionary<t, kdict1, Dictionry<t, k>
            dict2)
            {
            ...
            }

            Dictionary<int, stringa;
            Dictionary<int, stringb;
            ....
            Dictionary<int, stringc = Dictionary.Conc at(a, b);

            but no one seems to agree with me about grouping related functions with
            static extensions!

            Dict


            "Peter Larsen [CPH]" wrote:
            Hi,
            >
            How do i concat two dictionaries aka the following sample:
            >
            Dictionary<int, stringa;
            Dictionary<int, stringb;
            b.Add(a);
            >
            What is the easiest way to concat two dictionaries ??
            >
            BR
            Peter
            >
            >
            >

            Comment

            • Peter Larsen [CPH]

              #7
              Re: Concat two Dictionary&lt;& gt;

              Thank you for you comments.
              I think my question has been answered by now.

              /Peter



              Comment

              • Cowboy \(Gregory A. Beamer\)

                #8
                Re: Concat two Dictionary&lt;& gt;

                One of the best, human readible that is, explanations of Lambda expressions
                is found in the book Introducing Microsoft(r) LINQ. You can read Chapter 2
                from the free book offer page (http://csna01.libredigital.com/).

                It runs through Generics to Delegates to Anonymous Methods to Extension
                Methods to Lambda Expressions. Once you follow the path, you should have a
                decent idea of how to navigate this jungle.

                --
                Gregory A. Beamer
                MVP, MCP: +I, SE, SD, DBA

                Subscribe to my blog


                or just read it:


                *************** *************** *************** ****
                | Think outside the box!
                |
                *************** *************** *************** ****
                "Peter Larsen [CPH]" <PeterLarsen@co mmunity.nospamw rote in message
                news:OsbNoEAlIH A.1768@TK2MSFTN GP05.phx.gbl...
                >I am aware about the potential danger in the sample - i'm just trying to
                >figure out what linq and lambda is and what it can do for me :-)
                >
                /Peter
                >
                "Cowboy (Gregory A. Beamer)" <NoSpamMgbworld @comcast.netNoS pamMwrote in
                message news:ObNqIy$kIH A.980@TK2MSFTNG P06.phx.gbl...
                >Are their any potential clashes in the key? In other words, suppose the
                >following:
                >>
                >Dictionary<int , stringa = new Dictionary<int, string>();
                >Dictionary<int , stringb = new Dictionary<int, string>();
                >>
                >a.Add(1,"a") ;
                >a.Add(2,"b") ;
                >>
                >b.Add(1, "c");
                >b.Add(2, "d");
                >>
                >You have a clash here. Although you can concat in one statement, in
                >Framework 3.5, you will end up with a clash in keys, which is not good.
                >If b is filled like so:
                >>
                >b.Add(3, "c");
                >b.Add(4, "d");
                >>
                >you do not end up with the same issue.
                >>
                >If you have clashes, you will have to loop.
                >>
                >--
                >Gregory A. Beamer
                >MVP, MCP: +I, SE, SD, DBA
                >>
                >Subscribe to my blog
                >http://gregorybeamer.spaces.live.com/lists/feed.rss
                >>
                >or just read it:
                >http://gregorybeamer.spaces.live.com/
                >>
                >>
                >

                Comment

                • Peter Larsen [CPH]

                  #9
                  Re: Concat two Dictionary&lt;& gt;

                  Thanks for the links Gregory..

                  /Peter

                  "Cowboy (Gregory A. Beamer)" <NoSpamMgbworld @comcast.netNoS pamMwrote in
                  message news:u0IYGyalIH A.944@TK2MSFTNG P05.phx.gbl...
                  One of the best, human readible that is, explanations of Lambda
                  expressions is found in the book Introducing Microsoft(r) LINQ. You can
                  read Chapter 2 from the free book offer page
                  (http://csna01.libredigital.com/).
                  >
                  It runs through Generics to Delegates to Anonymous Methods to Extension
                  Methods to Lambda Expressions. Once you follow the path, you should have a
                  decent idea of how to navigate this jungle.
                  >
                  --
                  Gregory A. Beamer
                  MVP, MCP: +I, SE, SD, DBA
                  >
                  Subscribe to my blog

                  >
                  or just read it:

                  >

                  Comment

                  Working...