如何在集合中正确使用ForMember?

时间:2016-04-12 08:07:21

标签: c# automapper

我的源类是

public class SourceEmployee
    {
        public int EmployeeID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public List<ResidentialAddress> EmployeeResidences { get; set; }
    }

ResidentialAddress

之下
public class ResidentialAddress
    {
        public string State { get; set; }
        public string City { get; set; }
        public int ZipCode { get; set; }

    }

目的地类位于

之下
public class DestinationEmployee
{
        public int EmployeeID { get; set; }
        public string FullName { get; set; }       
        public List<ResidentialAddress1> Address { get; set; }

}

public class ResidentialAddress1
{
   public string FullAddress { get; set; }
}

如何为FullAddress执行ForMember State + City + ZipCode

我在

之后迷路了
Mapper.CreateMap<SourceEmployee, DestinationEmployee>();

Mapper.CreateMap<SourceEmployee, DestinationEmployee>().
ForMember(f => f.FullName, f => f.MapFrom(a => string.Concat(a.FirstName, " ", a.LastName)))
.ForMember(x => x.EmployeeResidences1, x => x.MapFrom(y => string.Concat(y.EmployeeResidences.m, " ", y.LastName)));

2 个答案:

答案 0 :(得分:2)

那么,您可以使用LINQ让AutoMapper知道如何将3个属性映射到一个属性中,并且您不应该使用Mapper.CreateMap(),因为它已被弃用并且不会从版本5.0支持 - 改为使用Mapper.Initialize()

让我们来看看这个例子:

            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap<SourceEmployee, DestinationEmployee>();

                cfg.CreateMap<SourceEmployee, DestinationEmployee>()
                .ForMember(f => f.FullName, f => f.MapFrom(a => string.Concat(a.FirstName, " ", a.LastName)))
                .ForMember(
                    x => x.Address,
                    x => x.MapFrom(
                        y => y.EmployeeResidences.Select(
                            r => new ResidentialAddress1()
                            {
                                FullAddress = String.Concat(
                                    r.State, "  ", r.City, "  ", r.ZipCode)
                            }).ToList()));
            });

            SourceEmployee emp = new SourceEmployee()
            {
                EmployeeID = 1,
                FirstName = "Alex",
                LastName = "Green",
                EmployeeResidences = new List<ResidentialAddress>()
                {
                    new ResidentialAddress() { State = "abc", City = "def", ZipCode = 110 },
                    new ResidentialAddress() { State = "foo", City = "qwe", ZipCode = 220 },
                    new ResidentialAddress() { State = "bar", City = "ert", ZipCode = 330 },
                }
            };

            var sourceEmp = Mapper.Map<SourceEmployee, DestinationEmployee>(emp);

            Console.WriteLine(sourceEmp.Address.Count);    
            Console.WriteLine(sourceEmp.Address[1].FullAddress);

输出:

3
foo qwe 220

答案 1 :(得分:1)

您应该为ResidentialAddress -> ResidentialAddress1转换设置单独的地图配置文件。使用automapper转换父对象时,将使用定义的地图配置文件转换所有子对象:

Mapper.CreateMap<SourceEmployee, DestinationEmployee>()
    .ForMember(f => f.FullName, f => f.MapFrom(a => string.Concat(a.FirstName, " ", a.LastName)))
    .ForMember(x => x.Address, x => x.MapFrom(y => y.EmployeeResidences)));

Mapper.CreateMap<ResidentialAddress, ResidentialAddress1>
    .ForMember(x => x.FullAddress, map=>map.From(from => string.Format("{0} {1} {2}", from.State, from.City, from.ZipCode);

这样,如果在代码中您必须在多个位置将ResidentialAddress转换为ResidentialAddress1,则无法添加任何代码,只需使用Mapper.Map<>

我还建议您切换到配置文件,而不是内联定义映射器配置:https://github.com/AutoMapper/AutoMapper/wiki/Configuration