Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8215847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:55:24+00:00 2026-06-07T11:55:24+00:00

Automapper easily handles mapping one list of object types to another list of different

  • 0

Automapper easily handles mapping one list of object types to another list of different objects types, but is it possible to have it map to an existing list using an ID as a key?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T11:55:26+00:00Added an answer on June 7, 2026 at 11:55 am

    I have not found better way than the following.

    Here are source and destination.

    public class Source
    {
        public int Id { get; set; } 
        public string Foo { get; set; }
    }
    
    public class Destination 
    { 
        public int Id { get; set; }
        public string Foo { get; set; }
    }
    

    Define converter (You should change List<> to whatever type you are using).

    public class CollectionConverter: ITypeConverter<List<Source>, List<Destination>>
    {
        public List<Destination> Convert(ResolutionContext context)
        {
            var destinationCollection = (List<Destination>)context.DestinationValue;
            if(destinationCollection == null)
                destinationCollection = new List<Destination>();
            var sourceCollection = (List<Source>)context.SourceValue;
            foreach(var source in sourceCollection)
            {
                Destination matchedDestination = null;
    
                foreach(var destination in destinationCollection)
                {
                    if(destination.Id == source.Id)
                    {
                        Mapper.Map(source, destination);
                        matchedDestination = destination;
                        break;
                    }
                }
                if(matchedDestination == null)
                    destinationCollection.Add(Mapper.Map<Destination>(source));
            }
            return destinationCollection;
        }
    }
    

    And here is actual mapping configuration and example.

    Mapper.CreateMap<Source,Destination>();
    Mapper.CreateMap<List<Source>,List<Destination>>().ConvertUsing(new CollectionConverter());
    
    var sourceCollection = new List<Source>
    {
        new Source{ Id = 1, Foo = "Match"},
        new Source{ Id = 2, Foo = "DoesNotMatchWithDestination"}
    };
    var destinationCollection = new List<Destination>
    {
        new Destination{ Id = 1, Foo = "Match"},
        new Destination{ Id = 3, Foo = "DoeNotMatchWithSource"}
    };
    var mergedCollection = Mapper.Map(sourceCollection, destinationCollection);
    

    You should get the following result.

    Mapping result

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use AutoMapper to map my domain objects to my view models. I have
AutoMapper for .Net lets you map from one type to another. Its most basic
I'm trying to use Automapper to map to objects, the issue is one of
Can Automapper map values from a NameValueCollection onto an object, where target.Foo receives the
How can I use automapper to update the properties values of another object without
I'm a complete noob to Automapper and I'm wondering if its possible to map
Using AutoMapper: When mapping nested collections, I expect any unmapped properties to have their
I am using Automapper to map between Entity and ViewModel object (in both directions).
Calling all AutoMapper gurus! I'd like to be able to map object A to
I have an AutoMapper profile which currently depends on UrlHelper for the following mapping:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.