我有以下列表。我想将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
属性?
是否有更短的方法来忽略属性而不是逐个列出它们?