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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:09:58+00:00 2026-05-26T15:09:58+00:00

Issue Seems like the condition gets ignored. Here is my scenario: Source class public

  • 0

Issue
Seems like the condition gets ignored. Here is my scenario:

Source class

public class Source
{
    public IEnumerable<Enum1> Prop1{ get; set; }

    public IEnumerable<Enum2> Prop2{ get; set; }

    public IEnumerable<Enum3> Prop3{ get; set; }
}

The enums subclass from a byte and are decorated with [Flags]. The destination class simply contains properties like Enum1, Enum2 and Enum3 containging the “total” bitwise value. So in essense if the Enumeration contains Enum1.value!, Enum1.Value2 and Enum1.Value3, the destination will contain the bitwise value of Enum1.Value1 | Enum1.Value2 | Enum1.Value3

Destination class

    public Enum1 Prop1 { get; set; }

    public Enum2 Prop2 { get; set; }

    public Enum3 Prop3 { get; set; }

AutoMapper Mapping

    Mapper.CreateMap<Source, Destination>()
            .ForMember(m => m.Prop1, o =>
                {
                    o.Condition(c => !c.IsSourceValueNull);
                    o.MapFrom(f => f.Prop1.Aggregate((current, next) => current | next));
                })
            .ForMember(m => m.Prop2, o =>
            {
                o.Condition(c => !c.IsSourceValueNull);
                o.MapFrom(f => f.Prop2.Aggregate((current, next) => current | next));
            })
            .ForMember(m => m.Prop3, o =>
            {
                o.Condition(c => !c.IsSourceValueNull);
                o.MapFrom(f => f.Prop3.Aggregate((current, next) => current | next));
            });  

The mapping works fine when the inner properties are not null and mapping succeeds and sets destination correctly. However, I want to skip the mapping when the member source value is null (when Prop1 is null, then skip the mapping).

I can see from debugging that Source.Prop1 is null. The condition gets completely ignored and get the exception saying the value is null.

Trying to map Source to Destination. Destination property: Prop1. Exception of type 'AutoMapper.AutoMapperMappingException' was thrown. --> Value cannot be null. Parameter name: source

I’m not sure if IsSourceValueNull checking for Prop1 or the the actual Source class which is not null. Only the member Prop1 is null.

Any help isappreciated.

  • 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-26T15:09:59+00:00Added an answer on May 26, 2026 at 3:09 pm

    I think you need to do the Condition and MapFrom in two steps:

    .ForMember(c => c.Prop1, o => o.Condition(c => !c.IsSourceValueNull));
    .ForMember(c => c.Prop1, o => o.MapFrom(f => f.Prop1.Aggregate(...));
    

    The MapFrom will never be used if the Condition evaluates to false.

    EDIT

    Hmmm… That doesn’t seem to work. I thought I had used that before somewhere. You could resort to just the MapFrom:

    .MapFrom(f => f.Prop1 == null ? null : f.Prop1.Aggregate(...));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue that seems like very flaky behavour, is this a problem
I've got a weird issue that almost seems like a Visual Studio 2008 issue.
This seems like an easy enough issue but I can't seem to find the
This seems like it would be a common issue to be but I don't
Seems like the subtraction is triggering some kind of issue and the resulting value
I've been investigating this issue that only seems to get worse the more I
I have a while loop issue in PHP that seems like it could have
I have an issue where I have something like this: class Thing(models.Model): def can_vote(self,
I have a weird issue that only seems to be affecting IE 7. The
There seems to be an issue simulating the backspace key with java.awt.Robot . This

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.