0

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?

adam78
  • 9,668
  • 24
  • 96
  • 207
  • 2
    How would you call this mapping? AutoMapper isn't the right tool for updating an existing list from another list. – Jasen Nov 01 '17 at 21:44
  • You might instead do a Linq join on `Name` and select the fields you want for the new list. – Jasen Nov 01 '17 at 21:47
  • @Jasen I agree with you. I've just gone a done it using a foreach loop instead. sometimes automapper seems more trouble than its worth. – adam78 Nov 01 '17 at 22:05

0 Answers0