AutoMapper - 将2个对象列表合并为1个列表

时间:2016-05-24 14:03:20

标签: c# automapper

我见过一些示例,您可以使用AutoMapper将来自多个来源的数据合并到一个来源。

我的问题是,当我处理数据列表时,如何才能实现这一目标?

编辑:在我的示例中,List(Person)将映射到List(PersonCompany),但是List(Company)到List(PersonCompany)的映射不会映射。

编辑2:更改了映射器以更新模型,但公司映射现在是唯一被映射的数据。

说我有以下课程:

public class Person
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string PhNo { get; set; }
}
public class Company
{
    public int PersonID { get; set; }
    public int EmpNo { get; set; }
    public string Title { get; set; }
}

public class PersonCompany
{
    public string Name { get; set; }
    public string PhNo { get; set; }

    public int EmpNo { get; set; }
    public string Title { get; set; }

    public int ID { get; set; }
}

现在这个简单的例子有用......

    // This is a simple example using AutoMapper to map 2 source objects into one destination object.
    var personMapConfig = new MapperConfiguration(c =>
        c.CreateMap<Person, PersonCompany>()
        .ForMember(x => x.Name, y => y.MapFrom(a => a.Name))
        .ForMember(x => x.PhNo, y => y.MapFrom(a => a.PhNo))
        .ForMember(x => x.ID, y => y.MapFrom(a => a.ID))
        );

    var companyMapConfig = new MapperConfiguration(c =>
    c.CreateMap<Company, PersonCompany>()
    .ForMember(d => d.EmpNo, opt => opt.MapFrom(s => s.EmpNo))
    .ForMember(d => d.Title, opt => opt.MapFrom(s => s.Title))
    );

    var person = new Person { ID=1, Name = "PersonName", PhNo = "212-000-0000" };            
    var company = new Company { PersonID = 1, Title = "Associate Director", EmpNo = 10001 };


    var model = personMapConfig.CreateMapper().Map<Person, PersonCompany>(person);
    companyMapConfig.CreateMapper().Map<Company, PersonCompany>(company, model);

然而,当我使用数据列表时,却没有。

    var personList = new List<Person> { new Person { ID=1, Name="David Johnson", PhNo="(111) 111-1111"},
                                        new Person { ID=2, Name="Marvin Miller", PhNo="(222) 222-2222"},
                                        new Person { ID=3, Name="Jack Wilson", PhNo="(333) 333-3333"}};

    var companyList = new List<Company> { new Company { EmpNo= 1, PersonID = 1, Title="Director"},
                                        new Company { EmpNo= 2, PersonID = 2, Title="Surgeon"},
                                        new Company { EmpNo= 3, PersonID = 3, Title="Sales"}};

    var personMapConfig = new MapperConfiguration(c =>
        c.CreateMap<Person, PersonCompany>()
        .ForMember(x => x.Name, y => y.MapFrom(a => a.Name))
        .ForMember(x => x.PhNo, y => y.MapFrom(a => a.PhNo))
        .ForMember(x => x.ID, y => y.MapFrom(a => a.ID))
    );

    var companyMapConfig = new MapperConfiguration(c =>
        c.CreateMap<Company, PersonCompany>()
        .ForMember(d => d.EmpNo, opt => opt.MapFrom(s => s.EmpNo))
        .ForMember(d => d.Title, opt => opt.MapFrom(s => s.Title))
    );

    var model = personMapConfig.CreateMapper().Map<List<Person>, List<PersonCompany>>(personList);
    companyMapConfig.CreateMapper().Map<List<Company>, List<PersonCompany>>(companyList, model);

2 个答案:

答案 0 :(得分:1)

您的最后两行称为.Map<List<Person>, List<PersonCompany>().Map<List<Company>, List<PersonCompany>>()。这两个调用都会返回您期望的值。问题是您没有加入数据。您只告诉它如何转换Person - &gt; PersonCompanyCompany - &gt; PersonCompany,而不是如何将数据加入到一起。

我怀疑你是否需要使用AutoMapper。以下作品:

var personList = new List<Person> { new Person { ID=1, Name="David Johnson", PhNo="(111) 111-1111"},
                                        new Person { ID=2, Name="Marvin Miller", PhNo="(222) 222-2222"},
                                        new Person { ID=3, Name="Jack Wilson", PhNo="(333) 333-3333"}};

var companyList = new List<Company> { new Company { EmpNo= 1, PersonID = 1, Title="Director"},
                                    new Company { EmpNo= 2, PersonID = 2, Title="Surgeon"},
                                    new Company { EmpNo= 3, PersonID = 3, Title="Sales"}};

var personCompanies = companyList.Join(personList, m => m.PersonID, m => m.ID, (c, p) => new PersonCompany {
    Name = p.Name,
    PhNo = p.PhNo,
    EmpNo = c.EmpNo,
    ID = p.ID,
    Title = c.Title
});

答案 1 :(得分:0)

  public static class MapperExtentions
  {
    public static TResult MergeInto<TResult>(this IMapper mapper, object item1, object item2)
    {
        return mapper.Map(item2, mapper.Map<TResult>(item1));
    }

    public static TResult MergeInto<TResult>(this IMapper mapper, params object[] objects)
    {
        var res = mapper.Map<TResult>(objects.First());
        return objects.Skip(1).Aggregate(res, (r, obj) => mapper.Map(obj, r));
    }
  }

   //How to use extentions
  var personList = new List<Person> { new Person { ID=1, Name="David Johnson", PhNo="(111) 111-1111"},
                                    new Person { ID=2, Name="Marvin Miller", PhNo="(222) 222-2222"},
                                    new Person { ID=3, Name="Jack Wilson", PhNo="(333) 333-3333"}};

  var companyList = new List<Company> { new Company { EmpNo= 1, PersonID = 1, Title="Director"},
                                new Company { EmpNo= 2, PersonID = 2, Title="Surgeon"},
                                new Company { EmpNo= 3, PersonID = 3, Title="Sales"}};

var personMapConfig = new MapperConfiguration(c =>
    c.CreateMap<Person, PersonCompany>()
    .ForMember(x => x.Name, y => y.MapFrom(a => a.Name))
    .ForMember(x => x.PhNo, y => y.MapFrom(a => a.PhNo))
    .ForMember(x => x.ID, y => y.MapFrom(a => a.ID))
);

var companyMapConfig = new MapperConfiguration(c =>
    c.CreateMap<Company, PersonCompany>()
    .ForMember(d => d.EmpNo, opt => opt.MapFrom(s => s.EmpNo))
    .ForMember(d => d.Title, opt => opt.MapFrom(s => s.Title))
);

var model = personMapConfig.CreateMapper().Map<List<Person>, List<PersonCompany>>(personList);
companyMapConfig.CreateMapper().Map<List<Company>, List<PersonCompany>>(companyList, model);

    var result = companyList.Join(personList, s => s.PersonID, t => t.ID, (c, p) => _mapper.MergeInto<PersonCompany>(c, p));