agileobjects/AgileMapper

List Merging

Closed this issue · 7 comments

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

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:

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

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.
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

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! :)