Skip to content
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

Closed
t-andre opened this issue Mar 31, 2021 · 7 comments
Closed

List Merging #211

t-andre opened this issue Mar 31, 2021 · 7 comments
Assignees
Labels

Comments

@t-andre
Copy link

t-andre commented Mar 31, 2021

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

@SteveWilkes
Copy link
Member

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

@t-andre
Copy link
Author

t-andre commented Apr 1, 2021

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:

Id Name Description Status
2 Jane CustomerViewModel Description 2 Updated
1 Bungle CustomerViewModel Description 1 Updated
3 Zippy CustomerViewModel Description 3 Deleted
4 Freddy Customer Description 4 New

But I would like to have this one:

Id Name Description Status
2 Jane Customer Description 2 Updated
1 Rod Customer Description 1 Updated
3 Zippy CustomerViewModel Description 3 Deleted
4 Freddy Customer Description 4 New

Thanks in advance

@SteveWilkes
Copy link
Member

SteveWilkes commented Apr 3, 2021

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

@t-andre
Copy link
Author

t-andre commented Apr 5, 2021

Thanks for the response.

Tried the Map Over.
With simple list I can go and check the deleted record, but I have many lists and it can be difficult to check then all.
The function I need if it's possible is to have Map Over and let the deleted record in the list.

Cheers

@SteveWilkes SteveWilkes self-assigned this Apr 6, 2021
@SteveWilkes
Copy link
Member

SteveWilkes commented Apr 6, 2021

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

@t-andre
Copy link
Author

t-andre commented Apr 7, 2021

Hi,

Thank you for your help.

@SteveWilkes
Copy link
Member

You're welcome! :)

SteveWilkes added a commit that referenced this issue Mar 19, 2022
#211 / Moving LangVersion to Build.Directory.props / Performance tweaks / Updating NuGet packages
SteveWilkes added a commit that referenced this issue Mar 31, 2022
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants