-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
List Merging #211
Comments
Hi! I'm not completely sure what behaviour you're after - could you add an example showing what you're looking to achieve? Cheers, Steve |
Hi, With this example: var mapper = Mapper.CreateNew();
var source = new List<Customer>
{
new Customer { Id = 1, Name = "Rod", Description = "Customer Description 1" },
new Customer { Id = 2, Name = "Jane" , Description = "Customer Description 2" },
new Customer { Id = 4, Name = "Freddy" , Description = "Customer Description 4" }
};
var target = new Collection<CustomerViewModel>
{
new CustomerViewModel { Id = 2, Name = null, Description = "CustomerViewModel Description 2" },
new CustomerViewModel { Id = 1, Name = "Bungle", Description = "CustomerViewModel Description 1" },
new CustomerViewModel { Id = 3, Name = "Zippy", Description = "CustomerViewModel Description 3" }
};
var result = mapper.Map(source).OnTo(target); Have this result:
But I would like to have this one:
Thanks in advance |
Hi, I think what you're after is an Overwrite mapping - instead of: mapper.Map(source).OnTo(target); ...use: mapper.Map(source).Over(target); I've put together a .NET Fiddle and I think it does what you describe. Let me know if not! Cheers, Steve |
Thanks for the response. Tried the Map Over. Cheers |
hi, There's nothing out of the box - if I've understood what you're after correctly - but because the mapper identifies objects by ID and reuses them, you could try: var mapper = Mapper.CreateNew();
var source = new List<Customer>
{
new Customer { Id = 1, Name = "Rod", Description = "Customer Description 1" },
new Customer { Id = 2, Name = "Jane" , Description = "Customer Description 2" },
new Customer { Id = 4, Name = "Freddy" , Description = "Customer Description 4" }
};
var target = new Collection<CustomerViewModel>
{
new CustomerViewModel { Id = 2, Name = null, Description = "CustomerViewModel Description 2" },
new CustomerViewModel { Id = 1, Name = "Bungle", Description = "CustomerViewModel Description 1" },
new CustomerViewModel { Id = 3, Name = "Zippy", Description = "CustomerViewModel Description 3" }
};
// Use .ToList() to map over a List<T> copy of the target instead of the original Collection<T>:
var result = mapper.Map(source).Over(target.ToList());
// Get the objects which exist in the original target Collection<T>, but not the result List<T>:
var removedObjects = target.Except(result);
// Re-add the removed objects to the mapped List<T>:
result.AddRange(removedObjects);
// If you need a Collection<T> instead of a List<T>,
// re-assign 'target' to a new Collection<T> containing the objects:
target = new Collection<CustomerViewModel>(result); Hope that helps, Steve |
Hi, Thank you for your help. |
You're welcome! :) |
#211 / Moving LangVersion to Build.Directory.props / Performance tweaks / Updating NuGet packages
* Fixing mapping of ctor parameters named eactly the same as members, re: #211 / Moving LangVersion to Build.Directory.props / Performance tweaks / Updating NuGet packages * Renames for clarity * Handling named, indexed VB.NET properties, re: #221 * Support for VB.NET named, indexed properties where all indexes have default values, re: #221 / Performance improvements * Fixing cache concurrency * Switching .NET Core test projects to LTS versions * Adding .NET 5 + 6 test projects * Updating release notes * Handling named, indexed property null optional default index values, re: #211 * Tweak * Updating to v1.8.1 * Removing NuGet pack bat file * Add v1.8.1 NuGet package
Hi,
It's possible to merge two list and update all the properties?
Or update and let the deleted item in the target list?
Thanks
The text was updated successfully, but these errors were encountered: