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

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

    }

Destination 类如下

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

如何为将是 State + City + ZipCode 的 FullAddress 执行 ForMember?

我迷路了

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

最佳答案

好吧,您可以使用 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

关于c# - 如何在集合中正确使用 ForMember?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36567201/

相关文章:

c# - 自动映射器:忽略条件

c# - automapper 条件自定义值解析器

c# - 寻找特定日期

c# - 为什么我的 Visual Studio 会自动关闭而没有任何错误

c# - 将关键字等效项从 Java 更新为 C#

c# - AutoMapper 使用错误的构造函数

c# - 带有 Automapper 的 EF Core 抛出异常 'Entity type cannot be tracked'

c# - 基于 AutoMapper 约定的系统

c# - 有没有办法从 IL 代码中获取 “reflect” ILGenerator.Emit 命令?

c# - 在C#中写行错误