How to combine 3 different list objects into a single list object?
-
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.
-
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.
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
-
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.
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.3modified on Thursday, May 26, 2011 12:43 PM
-
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.
Why do you have SalesTeamNorth, SalesTeamSouth and SalesTeamEast objects? Why wouldn't you just have a single SalesTeamMember or whatever object?
-
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.
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(); -
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.
-
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.
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.
-
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();Agreed. :)