Skip to content

Instantly share code, notes, and snippets.

@superman-lopez
Last active March 6, 2020 08:49
Show Gist options
  • Save superman-lopez/10d4a80ba7690554369a1d9e6041d67c to your computer and use it in GitHub Desktop.
Save superman-lopez/10d4a80ba7690554369a1d9e6041d67c to your computer and use it in GitHub Desktop.
Example of why I want to ignore item in collection
public class ObjectEntity
{
public int Id { get; set; }
public bool ReadOnly { get; set; }
public string SomeDescription { get; set; }
public double SomeValue { get; set; }
}
public class ObjectDto
{
public int Id { get; set; }
public bool ReadOnly { get; set; }
public string SomeDescription { get; set; }
public double SomeValue { get; set; }
}
public async Task<List<ObjectDto>> UpdateObjects(List<ObjectDto> dtoObjects)
{
List<ObjectEntity> objects = await context.Objects.ToListAsync();
objects = mapper.Map<List<ObjectDto>, List<ObjectEntity>>(dtoObjects, objects);
context.Objects.UpdateRange(objects);
await context.SaveChangesAsync();
List<ObjectEntity> objects = await context.Objects.ToListAsync();
dtoObjects = mapper.Map<List<ObjectEntity>, List<ObjectDto>>(objects);
return dtoObjects;
}
public ObjectMappingProfile()
{
CreateMap<ObjectDto, ObjectEntity>()
.IgnoreIf((od, oe) => {oe.ReadOnly == true;}) // Fictional function
.EqualityComparison((od, oe) => od.Id == oe.Id);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment