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

  • SEARCH
  • Home
  • 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 6636691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:13:04+00:00 2026-05-25T23:13:04+00:00

Hi All / very new to Auto-Mapper. i can map one to one objects

  • 0

Hi All / very new to Auto-Mapper. i can map one to one objects but was wondering is it possible to map multiple objects to one object or multiple objects to multiple objects?

consider i have a following scenario…

User Model

public class User
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public Company Company { get; set; }  // 1 user work in 1 company
    }

Company Model

public class Company
        {
            public string CompanyName { get; set; }
            public string Website { get; set; }
            public ICollection<User> Users { get; set; }  // 1 Company can have many users
        }

UserCompanyViewModel

i want to show List of users with their company details in one view..

public class UserCompanyViewModel
            {
                 public ICollection<User> Users { get; set; }
                 ppublic ICollection<Company> Companies { get; set; }   
            }

Now, is it possible to map in this situation and if yes, i can show in one view and when editing that view i want to map again with the updated fields back to their respective Models.

any help would be appreciated…thx

  • 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-05-25T23:13:04+00:00Added an answer on May 25, 2026 at 11:13 pm

    In this case, are you really using multiple (types of) objects as your source? It looks like from your defined problem that your source is a list of users – judging by “i want to show List of users with their company details”.

    If that’s the case, whilst you can’t do it implicitly you can use a TypeConverter to perform the map easily enough:

    Mapper.CreateMap<ICollection<User>, UserCompanyViewModel>()
          .ConvertUsing<UserCompanyViewModelConverter>();
    

    Then define your converter as:

    public class UserCompanyViewModelConverter : ITypeConverter<ICollection<User>, UserCompanyViewModel>
    {
        public UserCompanyViewModel Convert(ResolutionContext context)
        {
            UserCompanyViewModel model = new UserCompanyViewModel();
    
            ICollection<User> sourceUsers = (ICollection<User>)context.SourceValue;
    
            model.Users     = sourceUsers;
            model.Companies = sourceUsers.Select(u => u.Company).Distinct().ToList();
    
            return model;
        }
    }
    

    Then when you want to map you just take your collection of users someUsers and map it:

    UserCompanyViewModel model = Mapper.Map<ICollection<User>, UserCompanyViewModel>(someUsers);
    

    If you really do need to map multiple source types into a single destination type, it looks like this blog post includes a short Helper class that will help you. In short, AutoMapper doesn’t quite support this so you will be making a couple of Map requests to fill up your ViewModel. You will need to use another TypeConverter to make sure that the second call doesn’t replace the Companies added by the first.

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

Sidebar

Related Questions

I can't search for a particular string, since they're all very similar, but I'd
I am very new at all this c# Windows Phone programming, so this is
Very new to JQuery and MVC and webdevelopment over all. I'm now trying to
First of all, let me say I am very new to rails, have been
I've created a jQuery accordion for my website, it all works very fine. But
All the ways I can think to do this seem very hackish. What is
I'm very new with rails and I've been building a CMS application backend. All
I'm very new to Silverlight so I apologise if this question is obvious but
I am very new to ajax and jquery but I know some php. I
Firstly I am very new to all forms of javascript, particularly anything remotely AJAX.

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.