【问题标题】:Automapper Mapping List<T> to class containing TAutomapper 将 List<T> 映射到包含 T 的类
【发布时间】:2016-07-06 00:17:13
【问题描述】:

我有一堂课:

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

列表:

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

此列表包含 4 个分别名为“Person1”、“Person2”、“Person3”和“Person4”的 Person 对象以及属性的所有其他值。

现在我有另一个课程:

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();

现在我的问题是:如何将 personList 映射到 Citizen。我想将列表中的所有值加载到公民对象。请帮忙。

【问题讨论】:

    标签: c# linq automapper


    【解决方案1】:

    你的类应该是这样的

       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;}
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-09
      • 1970-01-01
      相关资源
      最近更新 更多