<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Automapper - TheCodeBuzz</title>
	<atom:link href="https://thecodebuzz.com/category/automapper/feed/" rel="self" type="application/rss+xml" />
	<link>https://thecodebuzz.com</link>
	<description>Best Practices for Software Development</description>
	<lastBuildDate>Fri, 06 Jan 2023 00:51:03 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://thecodebuzz.com/wp-content/uploads/2022/11/cropped-android-chrome-512x512-1-1-51x51.jpg</url>
	<title>Automapper - TheCodeBuzz</title>
	<link>https://thecodebuzz.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>C# .NET &#8211; Automapper map Array to Object with example</title>
		<link>https://thecodebuzz.com/automapper-map-array-to-object-example-dotnet-csharp/</link>
					<comments>https://thecodebuzz.com/automapper-map-array-to-object-example-dotnet-csharp/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sun, 28 Nov 2021 03:38:00 +0000</pubDate>
				<category><![CDATA[Automapper]]></category>
		<category><![CDATA[automapper - map list property]]></category>
		<category><![CDATA[automapper map array to object]]></category>
		<category><![CDATA[automapper map child list of objects]]></category>
		<category><![CDATA[automapper map list]]></category>
		<category><![CDATA[automapper map list of objects c#]]></category>
		<category><![CDATA[automapper map list to list c#]]></category>
		<category><![CDATA[automapper map list to single object]]></category>
		<category><![CDATA[automapper map nested list of objects]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=19569</guid>

					<description><![CDATA[<p>Automapper map Array to Object with example Today in this article, we will see how to perform Automapper map Array to Object of a different type. Today in this article, we will cover below aspects, To keep it simple, we shall use very simple examples and later look into all possible combinations of the map [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/automapper-map-array-to-object-example-dotnet-csharp/">C# .NET – Automapper map Array to Object with example</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading"><strong>Automapper map Array to Object with example</strong></h1>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="1024" height="320" src="https://www.thecodebuzz.com/wp-content/uploads/2021/12/Automapper-Map-Array-object-to-Array-object-C-.NET_-1024x320.jpg" alt="Automapper Map Array to object map" class="wp-image-19525" srcset="https://thecodebuzz.com/wp-content/uploads/2021/12/Automapper-Map-Array-object-to-Array-object-C-.NET_-1024x320.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/12/Automapper-Map-Array-object-to-Array-object-C-.NET_-300x94.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/12/Automapper-Map-Array-object-to-Array-object-C-.NET_-768x240.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/12/Automapper-Map-Array-object-to-Array-object-C-.NET_-1536x480.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2021/12/Automapper-Map-Array-object-to-Array-object-C-.NET_-785x245.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2021/12/Automapper-Map-Array-object-to-Array-object-C-.NET_.jpg 1855w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p><strong><em>To</em></strong>day in this article, we will see how to perform <strong>Automapper map Array to Object</strong> of a different type.</p>



<p></p>



<p>Today in this article, we will cover below aspects,</p>



<p></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-what-is-automapper">What is AutoMapper</a></li><li><a href="#aioseo-di-imapper-into-the-module">DI IMapper into the module</a></li><li><a href="#aioseo-automapper-map-array-to-object-configuring-profile-instances">Automapper map Array to Object &#8211; Configuring Profile Instances</a></li></ul></div>



<p></p>



<p>To keep it simple, we shall use very simple examples and later look into all possible combinations of the map <strong><em>array to object </em></strong>or map <strong><em>object to an array</em></strong> or even map array to array mapping examples.</p>



<p></p>



<p>We already learned in our article on <strong><a href="https://www.thecodebuzz.com/configure-automapper-asp-net-core-profile-map-object/" title="Configure Automapper in ASP.NET Core – Getting Started" target="_blank" rel="noreferrer noopener"><em>Getting started with Automapper in ASP.NET Core</em></a>.</strong></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-what-is-automapper">What is AutoMapper </h2>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><strong><em>AutoMapper </em></strong>is an object mapper that helps you transform one object of one type into an output object of a different type. This type of transformation is often needed when you work on business logic.</p>



<p></p>



<p>Automapper helps to&nbsp;<strong><em>centralize your repeated</em></strong>&nbsp;boring mapping logic, address the&nbsp;<strong><em>separation of concerns</em></strong>, provide&nbsp;<strong><em>better control on mapping</em></strong>&nbsp;making your&nbsp;<strong><em>code clean and easy to maintain</em></strong>.</p>
</blockquote>



<p></p>



<p></p>



<p>We shall be using the below classes where we have source type as &#8220;CheckingAccountSource&#8221; and destination as &#8220;CheckingAccountDestination&#8221;.</p>



<p></p>



<p>I have used the class naming convention as simple for understanding purposes.</p>



<p></p>



<p>Below is our sample source JSON file,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
{
  &quot;ID&quot;: &quot;Bank of ABCD&quot;,
  &quot;Branches&quot;: &#x5B;
    {
      &quot;Address&quot;: &#x5B;
        {
          &quot;StreetName&quot;: &quot;address1&quot;,
          &quot;ZipCode&quot;: &quot;Zip&quot;,
          &quot;ST&quot;: &quot;USA&quot;
        },
        {
          &quot;StreetName&quot;: &quot;address1&quot;,
          &quot;Zip&quot;: &quot;Zip&quot;,
          &quot;ST&quot;: &quot;USA&quot;
        }
      ]
    }
  ]
}

</pre></div>


<p></p>



<p>Below is the expected destination <strong><em>JSON </em></strong>output after mapping file,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
{
  &quot;Bank&quot;: &quot;Bank of ABCD&quot;,
  &quot;BranchInfo&quot;: &#x5B;
    {
        &quot;Location&quot;: 
          {
            &quot;Street&quot;: &quot;address1&quot;,
            &quot;Zip&quot;: &quot;Zip&quot;,
            &quot;State&quot;: &quot;USA&quot;
          }  
    }
  ]
}
</pre></div>


<p></p>



<p>Let&#8217;s define the Source and destination classes as below. here below our aim is to map the array from source to the object. </p>



<p></p>



<p>So nee here is to map only the first element from the array of objects to the destination object.</p>



<p></p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="456" src="https://www.thecodebuzz.com/wp-content/uploads/2021/12/Automapper-map-Array-to-Object-1024x456.jpg" alt="Automapper map Array to Object with example" class="wp-image-19571" srcset="https://thecodebuzz.com/wp-content/uploads/2021/12/Automapper-map-Array-to-Object-1024x456.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/12/Automapper-map-Array-to-Object-300x134.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/12/Automapper-map-Array-to-Object-768x342.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/12/Automapper-map-Array-to-Object-1536x685.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2021/12/Automapper-map-Array-to-Object-2048x913.jpg 2048w, https://thecodebuzz.com/wp-content/uploads/2021/12/Automapper-map-Array-to-Object-785x350.jpg 785w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p>In the above example, We have a very simple one-to-one mapping which can be achieved easily by creating the below Mapping profile.</p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-di-imapper-into-the-module"><em>DI IMapper into the module</em></h2>



<p></p>



<p>Dependency Injection of the <strong>IMapper </strong>interface is explained here already with more details,</p>



<p></p>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/configure-automapper-asp-net-core-profile-map-object/" target="_blank" rel="noreferrer noopener"><strong><em>Configure Automapper in ASP.NET Core – Getting Started</em></strong></a></li>
</ul>



<p></p>



<h2 class="wp-block-heading" id="aioseo-automapper-map-array-to-object-configuring-profile-instances">Automapper map Array to Object &#8211; Configuring Profile Instances</h2>



<p></p>



<p>Here you need to create a class that inherits from the class <em><strong>Profile</strong></em> then please add your configuration of mapping into the <em><strong>Constructor</strong></em>.</p>



<p></p>



<p>Profiles are one the best way of organizing all your mapping configurations in one place.</p>



<p></p>



<p>Below is <strong><em><a href="https://docs.automapper.org/" target="_blank" rel="noreferrer noopener">Automapper </a></em></strong>profile for the array of object mapping.</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; highlight: [6]; title: ; notranslate">
   public class CheckingAccountProfile : Profile
    {
        public CheckingAccountProfile()
        {
            CreateMap&lt;CheckingAccountSource, CheckingAccountDestination&gt;()
              .ForMember(dest =&gt; dest.Bank, o =&gt; o.MapFrom(src =&gt; src.ID))
              .ForMember(dest =&gt; dest.BranchInfo, o =&gt; o.MapFrom(src =&gt; src.Branches));

            CreateMap&lt;Branches, BranchInfo&gt;()
             .ForMember(dest =&gt; dest.Location, o =&gt; o.MapFrom(src =&gt; src.Address&#x5B;0]));

            CreateMap&lt;Address, Location&gt;()
            .ForMember(dest =&gt; dest.Street, o =&gt; o.MapFrom(src =&gt; src.ST))
            .ForMember(dest =&gt; dest.State, o =&gt; o.MapFrom(src =&gt; src.StreetName))
            .ForMember(dest =&gt; dest.Zip, o =&gt; o.MapFrom(src =&gt; src.ZipCode));

        }

..

..

}
</pre></div>


<p></p>



<p>Here is a simplified example to perform Mapping objects to an array of objects.</p>



<p></p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
        &#x5B;HttpPost]
        public IActionResult UpdateEmployee(&#x5B;FromBody] CheckingAccountSource soureObject)
        {
            var destObject = _mapper.Map&lt;Destination&gt;(soureObject);

            _logger.LogInformation(&quot;Mapping successful&quot;);

            return Ok(destObject);
        }

</pre></div>


<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="292" src="https://www.thecodebuzz.com/wp-content/uploads/2021/12/Automapper-map-Array-to-Object-example-1024x292.jpg" alt="Automapper Array to Object" class="wp-image-19573" srcset="https://thecodebuzz.com/wp-content/uploads/2021/12/Automapper-map-Array-to-Object-example-1024x292.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/12/Automapper-map-Array-to-Object-example-300x85.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/12/Automapper-map-Array-to-Object-example-768x219.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/12/Automapper-map-Array-to-Object-example-1536x438.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2021/12/Automapper-map-Array-to-Object-example-785x224.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2021/12/Automapper-map-Array-to-Object-example.jpg 1755w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p>Below are a few additional scenarios, which are covered in the link below,</p>



<p></p>



<p><strong><em>Scenario 1 </em></strong></p>



<p></p>



<p>Map an&nbsp;array of objects into a single object using an auto mapper.  For more details, please visit the below article &#8211; <strong><em> </em></strong></p>



<p></p>



<ul class="wp-block-list">
<li><strong><em><a href="https://www.thecodebuzz.com/automapper-map-single-object-to-array-of-object/" target="_blank" rel="noreferrer noopener" title="Automapper map object to an array of object">Automapper map object to an array of object</a></em></strong></li>
</ul>



<p></p>



<p></p>



<p><strong>Scenario 2</strong></p>



<p></p>



<p>Map an&nbsp;array of<strong><em> objects from the source to an array of object</em></strong>s using an auto mapper.</p>



<p> </p>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/automapper-map-array-to-array-object-to-array-list/" target="_blank" rel="noreferrer noopener" title="Automapper Map Array to an Array- Part 2"><strong><em>Automapper Map Array to an Array- Part 2</em></strong></a></li>
</ul>



<p></p>



<p></p>



<p><strong><em>Useful References :</em></strong></p>



<p></p>



<ul class="wp-block-list">
<li>  <strong><a href="https://www.thecodebuzz.com/configure-automapper-asp-net-core-profile-map-object/" title="Configure Automapper in ASP.NET Core – Getting Started" target="_blank" rel="noreferrer noopener"><em>Getting started with Automapper in ASP.NET Core</em></a></strong></li>
</ul>



<p></p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p><p>The post <a href="https://thecodebuzz.com/automapper-map-array-to-object-example-dotnet-csharp/">C# .NET – Automapper map Array to Object with example</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/automapper-map-array-to-object-example-dotnet-csharp/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
