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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:35:35+00:00 2026-05-28T19:35:35+00:00

Im get stuck with AutoMapper syntax. How to skip mapping members in the nested

  • 0

Im get stuck with AutoMapper syntax.

How to skip mapping members in the nested class (by condition string is empty)?
Im tried following code:

[TestMethod]
public void TestMethod4()
{
    var a = new A { Nested = new NestedA { V = 1, S = "A" } };
    var b = new B { Nested = new NestedB { V = 2, S = string.Empty } };

    Mapper.CreateMap<B, A>();   
    Mapper.CreateMap<NestedB, NestedA>().ForMember(s => s.S, opt => opt.Condition(src => !string.IsNullOrWhiteSpace(src.S)));
    var result = Mapper.Map(b, a);

      Assert.AreEqual(2, result.Nested.V);       // OK
      Assert.AreEqual("A", result.Nested.S);     // FAIL: S == null
}

Thanks

  • 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-28T19:35:35+00:00Added an answer on May 28, 2026 at 7:35 pm

    Have you tried using the opt.Skip suggested here.

    Mapper.CreateMap<NestedB, NestedA>()
     .ForMember(s => s.S, opt => opt.Skip(src => !string.IsNullOrWhiteSpace(src.S)));
    

    EDIT:

    After some digging in the source. I see that in the TypeMapObjectMapperRegistry class (the one that handles mappings for nested objects) it returns before seeing if the the destination value is needed to be preserved (using UseDestinationValue). Otherwise, I was going to suggest this:

    Mapper.CreateMap<B, A>();
                Mapper.CreateMap<NestedB, NestedA>()
                    .ForMember(s => s.S, opt => opt.Condition(src => !string.IsNullOrWhiteSpace(src.S)))
                    .ForMember(s => s.S, opt => opt.UseDestinationValue());
    

    I found this where Jimmy seems to address the core issue here.

    So, from what I’ve found, there doesn’t seem to be a way to use Condition and UseDestinationValue at the same time.

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

Sidebar

Related Questions

Sometimes I still get stuck trying to translate procedural code into functional code. Is
I'm not very good at C, and I always get stuck on simple string
I would like to use NHibernate Automapper to map the following class: public class
Why does my code: self.driver.find_element_by_xpath(//*[text()[contains(.,'%s')]] % ('Sorry')) Get stuck and won't pass this line?
When working on developing new software i normally get stuck with the redundancy vs
We have a lot of queries select * from tbl_message that get stuck on
I'm stuck trying to get a WinInet HTTP POST via SSL using ONLY C.
I'm stuck trying to get a regex to match a filetype in a sorting
I'm stuck on an aggregation problem that I can't get to the bottom of.
I've stuck myself in a c++ project under linux ,for which I get an

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.