Home:ALL Converter>C# Automapper, Two into One Based on ID

C# Automapper, Two into One Based on ID

Ask Time:2021-09-22T05:23:45         Author:Adam Rudzinski

Json Formatter

I recently started to use automapper, I got through the documentation but couldn't find or simply misunderstood some concepts. Well, my question is: Is there a way to merge two collections into one where source collections A and B have the same ID. Currently, I am mapping only from collection A, leaving some properties unassigned. I want to assign those properties from collection B. Summarizing, I'd like to get a collection C populated with objects containing AB based on one ID.

This is my code at the moment:

    var config = new MapperConfiguration(c => {
                c.AllowNullCollections = true;
                c.AllowNullDestinationValues = true;
                c.CreateMap<PaymentsReport, TaxCalculationData>();
            });
            var mapper = new Mapper(config);
            
            IEnumerable<TaxCalculationData> taxCalculationsX = mapper.Map<IEnumerable<TaxCalculationData>>(PaymentsReport).ToList();

Hopefully, my explanation is understandable, all suggestions will be very appreciated. Meantime I keep googling.

Author:Adam Rudzinski,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/69275711/c-sharp-automapper-two-into-one-based-on-id
yy