Last active
March 6, 2020 08:49
-
-
Save superman-lopez/10d4a80ba7690554369a1d9e6041d67c to your computer and use it in GitHub Desktop.
Example of why I want to ignore item in collection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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