Reputation: 15486
I have two lists. One list of Person and one list of Organization. I want to add these lists to each other and create a new list and then bind this list to a DropDownList. It is noteworthy that I want to add another property to this list that when I want to select an item from DropDownList, it give me this item is Person or Organization. How to I create this list?
Upvotes: 0
Views: 104
Reputation: 23218
Rereading your question, perhaps you shouldn't use Zip (or perhaps there's a better way than what I'm about to post using it).
Create a separate class that can contain both, have it mark if it's a person or organization, then build a list of those:
public class PersonOrganizationEntry
{
public Person Person { get; private set; }
public Organization Organization { get; private set; }
public PersonOrganizationType Type { get; private set; }
public PersonOrganizationEntry(Person person)
{
this.Person = person;
this.Type = PersonOrganizationType.Person;
}
public PersonOrganizationEntry(Organization organization)
{
this.Organization = organization;
this.Type = PersonOrganizationType.Organization;
}
}
public enum PersonOrganizationType
{
Person,
Organization
}
Then merge your lists together with this new type:
var persons = new List<Person>()
{
new Person(),
new Person(),
new Person()
};
var organizations = new List<Organization>()
{
new Organization(),
new Organization()
};
var mergedEntries =
persons.Select(p => new PersonOrganizationEntry(p))
.Concat(
organizations.Select(o => new PersonOrganizationEntry(o))
).ToList();
Then you can bind your drop-down-list against this collection of PersonOrganizationEntry
. Each "Person" entry will have an instance assigned to the entry's Person
field and the Type
will be PersonOrganizationType.Person
. Vice-versa for organizations.
Upvotes: 1