简体   繁体   中英

Automapper Mapping List<T> to class containing T

I have a class:

public class Person
{
 public string Name {get;set;}
 public string Address {get; set;}
 public string DOB {get; set;}
}

A list:

List<Person> personList = new List<Person>();

This List contains 4 Person objects with names "Person1", "Person2", "Person3" and "Person4" respectively and all other values for the properties.

Now I have another class:

public class Citizen
{
    public Person Abc1 {get; set;}= new Person{Name="Person1"};
    public Person Abc2 {get; set;}= new Person{Name="Person2"};
    public Person Abc3 {get; set;}= new Person{Name="Person3"};
    public Person Abc4 {get; set;}= new Person{Name="Person4"};
}

var citizen = new Citizen();

Now my question is: How do I map the personList to Citizen. I want to load all the values from the list to the citizen object. Please help.

You classes should look like this

   public class Citizen
    {
        public static List<Person> personList = new List<Person>() {
            new Person{Name="Person1"},
            new Person{Name="Person2"},
            new Person{Name="Person3"},
            new Person{Name="Person4"}
        };
    }
    public class Person
    {
     public string Name {get;set;}
     public string Address {get; set;}
     public string DOB {get; set;}
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM