AutoMapper Conditional Mapping in C#

AutoMapper Conditional Mapping in C# with Examples

In this article, I am going to discuss the AutoMapper Conditional Mapping in C# with some examples. The AutoMapper in C# allows us to add conditions to the properties of the source object that must be met before that property going to be mapped to the property of the destination object.

For example, if we want to map a property only if its value is greater than 0, then in such a situation we need to use AutoMapper Conditional Mapping in C#. Please read our previous article before proceeding to this article where we discussed the Reverse Mapping Using AutoMapper in C# with examples.

Let us understand AutoMapper Conditional Mapping in C# with an example.

We are going to use the following two classes in this demo.

AutoMapper Conditional Mapping in C#

Business Requirement:
  1. We need to Map the Name property of the Product class to the itemName property of the ProductDTO class only if the Name value starts with the letter “A”, else Map the OptionalName property value of the Product class with the ItemName property of the ProductDTO class.
  2. If the Quantity value is greater than 0 then only map it to the ItemQuantity
  3. Similarly, if the Amount value is greater than 100 then only map it to the Amount property of the ProductDTO class.

To achieve this we need to use the AutoMapper Conditional Mapping in C#. So Initializes the Mapper configuration as shown below to use the C# AutoMapper Conditional Mapping.

C# AutoMapper Conditional Mapping

The Condition option in AutoMapper is used to add the conditions to the properties that must be met before that property is going to map. The MapFrom option is used to perform the custom source and destination member mappings.

Below is the complete code.
using AutoMapper;
namespace AutoMapperDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            InitializeAutomapper();

            Product product = new Product()
            {
                ProductID = 101,
                Name = "Led TV",
                OptionalName = "Product name not start with A",
                Quantity = -5,
                Amount = 1000
            };

            var productDTO = Mapper.Map<Product, ProductDTO>(product);
            Console.WriteLine("After Mapping : Product");
            Console.WriteLine("ProductID : " + product.ProductID);
            Console.WriteLine("Name : " + product.Name);
            Console.WriteLine("OptionalName : " + product.OptionalName);
            Console.WriteLine("Quantity : " + product.Quantity);
            Console.WriteLine("Amount : " + product.Amount);

            Console.WriteLine();
            Console.WriteLine("After Mapping : ProductDTO");
            Console.WriteLine("ProductID : " + productDTO.ProductID);
            Console.WriteLine("ItemName : " + productDTO.ItemName);
            Console.WriteLine("ItemQuantity : " + productDTO.ItemQuantity);
            Console.WriteLine("Amount : " + productDTO.Amount);
            
            Console.ReadLine();
        }

        static void InitializeAutomapper()
        {
            Mapper.Initialize(config =>
            {
                config.CreateMap<Product, ProductDTO>()
                    //If the Name Start with A then Map the Name Value else Map the OptionalName value
                    .ForMember(dest => dest.ItemName, act => act.MapFrom(src => 
                        (src.Name.StartsWith("A") ? src.Name : src.OptionalName)))

                    //Take the quantity value if its greater than 0
                    .ForMember(dest => dest.ItemQuantity, act => act.Condition(src => (src.Quantity > 0)))

                    //Take the amount value if its greater than 100
                    .ForMember(dest => dest.Amount, act => act.Condition(src => (src.Amount > 100)));
            });
        }
    }

    public class Product
    {
        public int ProductID { get; set; }
        public string Name { get; set; }
        public string OptionalName { get; set; }
        public int Quantity { get; set; }
        public int Amount { get; set; }
    }

    public class ProductDTO
    {
        public int ProductID { get; set; }
        public string ItemName { get; set; }
        public int ItemQuantity { get; set; }
        public int Amount { get; set; }
    }
}
When we run the application, it displays the following output

AutoMapper Conditional Mapping in C# with examples

In the next article, I am going to discuss how to use the AutoMapper Ignore Property in C#. The C# AutoMapper Conditional Mapping is one of the important concepts of Automapper which is used in most of the real-time projects.

SUMMARY:

In this article, I try to explain AutoMapper Conditional Mapping in C# with some examples. I hope this AutoMapper Conditional Mapping in C# article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *