- Home /
How do you combine two lists of different classes into one?
I have two lists that I want to combine into one. One list has a class of member and one has a class for equipment.
I'd like each member to have their name and their equipment listed.
Tom Dick Harry
knife m-16 p90 pistol revolver Colt 45
Do I need a dictionary for this? I've tried a combination class CombineClass Both = new CombineClass(){MemberList=MemberList, EquipmentList=EquipmentList };
It compiles, but I don't know how to see how to check the data, like I could with a list.count. I know TUPLE is not supported. I've tried Ienumerable, concat, and cast using LINQ IEnumerable all = MemberList.Cast ().Concat (EquipmentList.Cast ());
Again, I don't know how to work with it to see if it did what I wanted it to. Anyone have any solutions? I'm open. Thanks!
Perhaps with a struct, that has a slot for member and another one for equipment and a list of that struct?
public class CombineClass : $$anonymous$$onoBehaviour
{
public List<$$anonymous$$embers> $$anonymous$$emberList;
public List<Equipment> EquipmentList;
}
Something like this?
more like this:
struct Pair
{
$$anonymous$$ember member;
Equipment equipment;
}
and then List<Pair> pairs
At that point how do I add the lists? Add? AddRange(which will only work with an iemumerator), put it into a dictionary or something else?
Answer by UnityCoach · Aug 16, 2017 at 05:55 PM
If you want a list of equipment for every member in the list, you can do :
Dictionary<Member, <List<Equipment>> membersData;
The use of $$anonymous$$ember is that the class or the initial list?
It's the type, so that you can store a dictionary of $$anonymous$$embers with their List of Equipment.
$$anonymous$$aybe I'm mistaken. What do you want to do? I mean why would you combine two lists of non comparable things?
I want to have a prefab that instantiates so it will display the name of the member and each piece of equipment they are using. The members are added separately to a database and displayed, thus list one. Then in another screen, you add equipment and display it, list two.
In the end all the data goes into a json file to be loaded in a mobile device. Does that make more sense?
Your answer

Follow this Question
Related Questions
Combining 2 lists in particular way... 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Grab a specific item from a list 3 Answers
Adding object to list add the same object to another list 1 Answer