I have the following Lists. I want to map the attributesList
to the fieldsList
list but I only want to update the Value
property in the fieldsList where the Name
values are equal. All the other properties in the fieldsList
should be ignored - I've only listed a few for brevity.
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())
How do I apply a condition to update the Values
property in the destination list where the Name
property in both source and destination match?
Is there a shorter way to ignore properties rather than listing them all one by one?