自动映射 - 条件列表映射

时间:2017-11-01 21:01:06

标签: asp.net-mvc automapper-5

我有以下列表。我想将attributesList映射到fieldsList列表,但我只想更新Value值相等的fieldsList中的Name属性。应忽略fieldsList中的所有其他属性 - 为简洁起见,我仅列出了一些属性。

public class Attribute {
       public int Id { get; set; }
       public string Name { get; set; }
       public string Value { get; set; }
}

public class Field {
       public string Name { get; set; }
       public string Value { get; set; }
       public string Type {get; set; }
       public string Style {get; set; }
}

var attributesList = new List<Attributes>
{
    new Field {Id = 1, Name = "Color", Value = "Blue"},
    new Field {Id = 2, Name = "Subject", Value = "Maths"}
};    

var fieldsList = new List<Field>
{
    new Field {Name = "Color", Value = "", Type = "Text", Style = "padding:10px"},
    new Field {Name = "Subject", Value = "", Type = "Text", Style = "padding:10px"}
};


 Mapper.CreateMap<List<Attributes>, List<Field>>()
    .ForMember(dest => dest.Name, opt => opt.Ignore()
   .ForMember(dest => dest.Type, opt => opt.Ignore()
  .ForMember(dest => dest.Style, opt => opt.Ignore())   

如何应用条件更新源和目标中Values属性匹配的目标列表中的Name属性?

是否有更短的方法来忽略属性而不是逐个列出它们?

0 个答案:

没有答案