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 7774051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:16:54+00:00 2026-06-01T17:16:54+00:00

I have a source model that looks like this: public class FooBar { public

  • 0

I have a source model that looks like this:

public class FooBar {
    public Foo foo {get;set;}
    public Bar bar {get;set;|
}

public class Foo {
    // tons of properties
}

public class Bar {
    // tons of irrelevant properties
    public Baz baz {get;set;}
}

Then I have a destination model that flattens FooBar into NewFooBar and looks like this:

public class NewFooBar {
    // all properties from Foo
    public Baz baz {get;set;}
}

How can I map FooBar to NewFooBar without having to individually map all of foo’s properties? (there are many of them).

My first solution was to Map Foo and Bar seperately to NewFooBar, and that works. Then perform the maps individually. But this seems kludgy. I’d rather do it in a single mapping if possible. Am I missing some simple way to do this?

  • 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-01T17:16:55+00:00Added an answer on June 1, 2026 at 5:16 pm

    Would this work?

    Mapper.CreateMap<Foo, NewFooBar>()
                .ForMember(dest => dest.baz, opt => opt.Ignore())
                ;
    
    Mapper.CreateMap<FooBar, NewFooBar>()
        .ConvertUsing(src => {
            var newFooBar = AutoMapper.Mapper.Map<Foo, NewFooBar>(src.foo);
    
            newFooBar.baz = src.bar.baz;
    
            return newFooBar;
        });
        ;
    

    and then

    var newFooBar = AutoMapper.Mapper.Map<FooBar, NewFooBar>(fooBar);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a model named Client and controller that looks like this class
I have an object that looks like this: [Bindable] public class MyRecord implements ValueObject
I have a model: class Example(models.Model): unique_hash = models.CharField(max_length=32,unique=True) content = models.FileField(upload_to='source',blank=True,verbose_name=HTML Content File)
I need to store some data that looks a little like this: xyz 123
I have multiple classes that I need to map into 1 class: This is
My application looks like this: main.py windows/ __init__.py mainwindow.py ... model/ __init__.py orders.py ...
I have source XMLfiles that come in with multiple root elements and there is
Have a source xml document that uses namespace containing prefixes and a default namespace.
I have a source XML file that is used to create C# files that
I have a source directory that includes a mix of different languages and files.

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.