Conditional Mapping
ℹ️ Description: Use Condition() to map properties only when certain conditions are met.
Conditional Mapping Results
Active User Result
| Property | Value |
|---|---|
| Full Name | John Doe |
| Email Mapped | john.doe@example.com |
Inactive User Result
| Property | Value |
|---|---|
| Full Name | John Doe |
| Email NOT Mapped | (empty - not mapped) |
🔀 Conditional Mapping Explained
CreateMap<User, UserBasicDTO>()
.ForMember(dest => dest.FullName,
opt => opt.MapFrom(src => $"{src.FirstName} {src.LastName}"))
.ForMember(dest => dest.Email,
opt => opt.Condition(src => src.IsActive)); // Only map if IsActive is true
// Result:
// Active User: Email is mapped
// Inactive User: Email is NOT mapped (remains empty/null)
Use Case: Hide sensitive information based on conditions, map only relevant data based on state, implement conditional business rules.