Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to combine 3 different list objects into a single list object?

How to combine 3 different list objects into a single list object?

Scheduled Pinned Locked Moved C#
tutorialquestion
8 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Goalie35
    wrote on last edited by
    #1

    I have 3 separate lists of objects, all containing the same properties: List teamNorth; List teamSouth; List teamEast; Is there a way to merge these 3 lists into a single list (i.e. "List allReps")? I was able to figure out how to merge 3 lists of the same object but not 3 lists of different objects. For most of my application, it's beneficial to keep these 3 objects separate, however there's one part in which having them merged into a single list object will save me from using lots of repetitive code. Thanks.

    P L S D V 6 Replies Last reply
    0
    • G Goalie35

      I have 3 separate lists of objects, all containing the same properties: List teamNorth; List teamSouth; List teamEast; Is there a way to merge these 3 lists into a single list (i.e. "List allReps")? I was able to figure out how to merge 3 lists of the same object but not 3 lists of different objects. For most of my application, it's beneficial to keep these 3 objects separate, however there's one part in which having them merged into a single list object will save me from using lots of repetitive code. Thanks.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Do they all share something like a base class, or even better, an interface? If they do, just use that as your type - the horrible alternative is to maintain it as a List of objects which, quite frankly, is no better than maintaining an ArrayList.

      Forgive your enemies - it messes with their heads

      My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

      1 Reply Last reply
      0
      • G Goalie35

        I have 3 separate lists of objects, all containing the same properties: List teamNorth; List teamSouth; List teamEast; Is there a way to merge these 3 lists into a single list (i.e. "List allReps")? I was able to figure out how to merge 3 lists of the same object but not 3 lists of different objects. For most of my application, it's beneficial to keep these 3 objects separate, however there's one part in which having them merged into a single list object will save me from using lots of repetitive code. Thanks.

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Even when your SalesTeamXyz classes would inherit from a common SalesTeam class, you wouldn't be able to simply cast a List<SalesTeamNorth> to a List<SalesTeam>, so the three lists have no common ancestor. [fixed angular brackets here]. Now do you really want those three classes? and even if you do, do you need the highly specialized lists? it would probably make more sense to have compatible lists, like so:

        List teamNorth;
        List teamSouth;
        List teamEast;

        while you still can store more specialized teams in them (with a risk of making some mistakes). Here is a complete example:

        class SalesTeam {
        public string name;
        }
        class SalesTeamNorth : SalesTeam {
        public SalesTeamNorth(int v) { name="N"+v; }
        }
        class SalesTeamSouth : SalesTeam {
        public SalesTeamSouth(int v) { name="S"+v; }
        }
        public override void Test(int arg) {
        List teamsNorth=new List();
        for(int i=0; i<3; i++) teamsNorth.Add(new SalesTeamNorth(i));
        List teamsSouth=new List();
        for (int i=0; i<3; i++) teamsSouth.Add(new SalesTeamSouth(i));

        // now iterate all teams
        foreach (List teams in new List\>(){ teamsNorth, teamsSouth }) {
        	foreach (SalesTeam team in teams) {
        		log(team.name);
        	}
        }
        

        }

        :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
        Please use <PRE> tags for code snippets, they improve readability.
        CP Vanity has been updated to V2.3

        modified on Thursday, May 26, 2011 12:43 PM

        1 Reply Last reply
        0
        • G Goalie35

          I have 3 separate lists of objects, all containing the same properties: List teamNorth; List teamSouth; List teamEast; Is there a way to merge these 3 lists into a single list (i.e. "List allReps")? I was able to figure out how to merge 3 lists of the same object but not 3 lists of different objects. For most of my application, it's beneficial to keep these 3 objects separate, however there's one part in which having them merged into a single list object will save me from using lots of repetitive code. Thanks.

          S Offline
          S Offline
          SledgeHammer01
          wrote on last edited by
          #4

          Why do you have SalesTeamNorth, SalesTeamSouth and SalesTeamEast objects? Why wouldn't you just have a single SalesTeamMember or whatever object?

          1 Reply Last reply
          0
          • G Goalie35

            I have 3 separate lists of objects, all containing the same properties: List teamNorth; List teamSouth; List teamEast; Is there a way to merge these 3 lists into a single list (i.e. "List allReps")? I was able to figure out how to merge 3 lists of the same object but not 3 lists of different objects. For most of my application, it's beneficial to keep these 3 objects separate, however there's one part in which having them merged into a single list object will save me from using lots of repetitive code. Thanks.

            D Offline
            D Offline
            dasblinkenlight
            wrote on last edited by
            #5

            Assuming that SalesTeamNorth, SalesTeamSouth, and SalesTeamEast are all classes inheriting from AllSalesReps, you could do it with LINQ:

            IList<AllSalesReps> allReps = teamNorth
            .Cast<AllSalesReps>()
            .Concat(teamSouth)
            .Concat(teamEast)
            .ToList();

            S 1 Reply Last reply
            0
            • G Goalie35

              I have 3 separate lists of objects, all containing the same properties: List teamNorth; List teamSouth; List teamEast; Is there a way to merge these 3 lists into a single list (i.e. "List allReps")? I was able to figure out how to merge 3 lists of the same object but not 3 lists of different objects. For most of my application, it's beneficial to keep these 3 objects separate, however there's one part in which having them merged into a single list object will save me from using lots of repetitive code. Thanks.

              V Offline
              V Offline
              V 0
              wrote on last edited by
              #6

              You could put everything in one class and add a property "category" eg. With LINQ you could then easily split the one list into the three you want. Just a quick idea.

              V.

              1 Reply Last reply
              0
              • G Goalie35

                I have 3 separate lists of objects, all containing the same properties: List teamNorth; List teamSouth; List teamEast; Is there a way to merge these 3 lists into a single list (i.e. "List allReps")? I was able to figure out how to merge 3 lists of the same object but not 3 lists of different objects. For most of my application, it's beneficial to keep these 3 objects separate, however there's one part in which having them merged into a single list object will save me from using lots of repetitive code. Thanks.

                B Offline
                B Offline
                BobJanova
                wrote on last edited by
                #7

                As others have said, there is no obvious reason why you should need a separate class for different groups of sales personnel. If they all have the same properties, and they are all semantically representing the same concept (a salesman), they are actually the same class. You can't merge lists of different classes. That's an additional incentive for you to rationalise this into being all one class.

                1 Reply Last reply
                0
                • D dasblinkenlight

                  Assuming that SalesTeamNorth, SalesTeamSouth, and SalesTeamEast are all classes inheriting from AllSalesReps, you could do it with LINQ:

                  IList<AllSalesReps> allReps = teamNorth
                  .Cast<AllSalesReps>()
                  .Concat(teamSouth)
                  .Concat(teamEast)
                  .ToList();

                  S Offline
                  S Offline
                  Sanjay J Patolia
                  wrote on last edited by
                  #8

                  Agreed. :)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups