AutoMapper / AutoMapper.Collection

AutoMapper support for updating existing collections by equivalency
MIT License
245 stars 59 forks source link

Following does not work as expected #134

Closed mnbuch closed 4 years ago

mnbuch commented 5 years ago

Class BEntity { ID, Name } Class B { ID, Name }

cfg.CreateMap<BEntity, B>().ReverseMap()
                   .EqualityComparison((be, b) => be.ID == b.ID);
var source= new List<B>()
 {
     new B() { ID = 1, Name = "B Entity 1"},
     new B() { ID = 2, Name = "B Entity 2"}
};

var dest = new List<BEntity>()
{
      new BEntity() { ID = 2, Name = "B Entity 2.1"},
      new BEntity() { ID = 3, Name = "B Entity 3"}
};

var list = mapper.Map(source, dest);

As far I understood list should have following 3 items:

{ID =1 , Name = B Entity 1} {ID =2 , Name = B Entity 2.1} {ID =3 , Name = B Entity3}

But this always has following {ID =2 , Name = B Entity 2} {ID =1 , Name = B Entity 1}

Is my understanding incorrect?

Tasteful commented 5 years ago

No, the target list will only contain the items that you have in the source item. If the item already exists in the target list (as id 2) then the same object will be reused.

mnbuch commented 5 years ago

The following is taken from the README.MD, which is not in sync with what you are saying.

If ID's match will map OrderDTO to Order
If OrderDTO exists and Order doesn't add to collection
If Order exists and OrderDTO doesn't remove from collection

image

Tasteful commented 5 years ago

It's the readme that not is correct, it think the text was meant as

Ping @TylerCarlson1