<?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[.net]]></category>
		<category><![CDATA[asp.net core]]></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. Updated November 2026: sections on collapsing an array into one object and on null, existing-collection and missing-map behaviour, plus an FAQ and a security upgrade note. Today in this [&#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 class="wp-block-paragraph"><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 class="tcb-update-note"><em>Updated November 2026: sections on collapsing an array into one object and on null, existing-collection and missing-map behaviour, plus an FAQ and a security upgrade note.</em></p>




<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Today in this article, we will cover below aspects,</p>



<p class="wp-block-paragraph"></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 class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph"></p>



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



<p class="wp-block-paragraph"></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p class="wp-block-paragraph"><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 class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">We shall be using the below classes where we have source type as &#8220;CheckingAccountSource&#8221; and destination as &#8220;CheckingAccountDestination&#8221;.</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">I have used the class naming convention as simple for understanding purposes.</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Below is our sample source JSON file,</p>



<p class="wp-block-paragraph"></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 class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Below is the expected destination <strong><em>JSON </em></strong>output after mapping file,</p>



<p class="wp-block-paragraph"></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 class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">So nee here is to map only the first element from the array of objects to the destination object.</p>



<p class="wp-block-paragraph"></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 class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p>



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



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Dependency Injection of the <strong>IMapper </strong>interface is explained here already with more details,</p>



<p class="wp-block-paragraph"></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 class="wp-block-paragraph"></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 class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Profiles are one the best way of organizing all your mapping configurations in one place.</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph"></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 class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Here is a simplified example to perform Mapping objects to an array of objects.</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></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 class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Below are a few additional scenarios, which are covered in the link below,</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"><strong><em>Scenario 1 </em></strong></p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph"></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 class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"><strong>Scenario 2</strong></p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph"> </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 class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"><strong><em>Useful References :</em></strong></p>



<p class="wp-block-paragraph"></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 class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph" 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 wp-block-paragraph"></p>



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



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



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



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p>



<hr>



<p class="wp-block-paragraph"></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 (2026) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p>



<h2>How do you collapse a string[] or object[] into one object with AutoMapper?</h2>

<p>AutoMapper has no default map from an array to a single object, because it can&#8217;t guess how the elements should combine. You have to say how, and you have two options: a <strong>global type converter</strong> or a <strong>per-member mapping</strong>.</p>

<h3>Global option: a type converter</h3>

<p>Declare <code>CreateMap&lt;string[], Target&gt;().ConvertUsing(...)</code> and you&#8217;ve defined the rule for that whole type pair. <code>ConvertUsing</code> accepts a <code>Func</code>, an <code>ITypeConverter</code> instance, or an <code>ITypeConverter</code> type. Use the type form when the converter <a href="https://thecodebuzz.com/how-to-inject-service-in-automapper-profile-class/">needs its own class or dependencies</a>.</p>

<blockquote class="tcb-plain-english" style="border-left:4px solid #f59e0b;background:#fffbeb;margin:18px 0;padding:10px 16px;font-style:normal"><p><strong>Note: </strong> A type converter is like a translator you hire for one language pair. Once hired, every document in that pair goes through them, whether you remember or not.</p></blockquote>

<p>The rule applies everywhere that pair is mapped, including nested properties. Choose it when the rule is identical on every call.</p>

<h3>Per-member option: MapFrom or a value resolver</h3>

<p>Use <code>ForMember</code> with <code>MapFrom</code> or a value resolver on the parent map. It affects one destination property only. Choose it when the array is a property of a larger object and the rule belongs to that map alone.</p>

<h3>Which one should you pick?</h3>

<p>The trade-off is reach against clarity. This table compares the two.</p>

<figure class="wp-block-table"><table>
<thead>
<tr><th>Approach</th><th>Scope</th><th>Best for</th></tr>
</thead>
<tbody>
<tr><td><strong>ConvertUsing</strong></td><td>Every map of that type pair</td><td>One rule used across the codebase</td></tr>
<tr><td><strong>ForMember + MapFrom</strong></td><td>One property on one map</td><td>A rule specific to that parent map</td></tr>
</tbody>
</table></figure>

<p>A converter is reusable, but it silently changes every map of that pair. <code>MapFrom</code> stays local, and a reader sees the rule right next to the member it fills. Default to <code>MapFrom</code> and move to a converter only when you catch yourself repeating the same expression.</p>

<h3>The same fix for &#8220;Missing type map configuration&#8221;</h3>

<p>The same idea resolves the error <em>Missing type map configuration or unsupported mapping</em>. It appears when AutoMapper has no map between two types, such as a <code>string</code> and a custom class. AutoMapper also has no built-in conversion from <code>string</code> to types like <code>int</code>, <code>DateTime</code> or <code>Type</code>. It throws at map time, and again when you validate the configuration, until you add a <code>CreateMap</code> with <code>ConvertUsing</code> for that pair.</p>

<h3>When AutoMapper isn&#8217;t worth it</h3>

<p>For a one-off conversion of a few elements, a plain LINQ <code>Select</code> or a hand-written constructor is shorter and easier to debug. You can step through it, and there&#8217;s no configuration to keep in sync. Reach for AutoMapper when the same mapping repeats across the codebase and a single definition saves you from copying it.</p>




<h2>Frequently asked questions</h2>

<h3><strong>Do I need to call CreateMap for the array type as well as the element type?</strong></h3>
<p>No. Only the element type needs a map. <code>CreateMap&lt;Source, Destination&gt;()</code> is enough to map <code>Source[]</code> to <code>Destination[]</code>, <code>List&lt;Destination&gt;</code>, <code>IList&lt;Destination&gt;</code>, <code>ICollection&lt;Destination&gt;</code> or <code>IEnumerable&lt;Destination&gt;</code>. Then call <code>mapper.Map&lt;Destination[]&gt;(sources)</code> or <code>mapper.Map&lt;List&lt;Destination&gt;&gt;(sources)</code>.</p>

<h3><strong>How do I map an array to a List&lt;T&gt; or IEnumerable&lt;T&gt; with AutoMapper?</strong></h3>
<p>Use the same element map and change the destination type argument, for example <code>mapper.Map&lt;Source[], List&lt;Destination&gt;&gt;(sources)</code>. No extra configuration is needed for the collection type. Swap <code>List&lt;Destination&gt;</code> for <code>IEnumerable&lt;Destination&gt;</code> and the same profile still works.</p>

<h3><strong>Why do I get &#8220;Missing type map configuration or unsupported mapping&#8221; when mapping an array?</strong></h3>
<p>AutoMapper has no map for the element pair, or you&#8217;re mapping a type it can&#8217;t convert on its own, such as a <code>string</code> to a custom class. Add a <code>CreateMap</code> for the element types, or add a <code>CreateMap</code> with <code>ConvertUsing</code> for the conversion. Calling <code>AssertConfigurationIsValid</code> in a unit test catches this early, before the mapping runs in production.</p>

<h3><strong>What happens when the source array is null?</strong></h3>
<p>A null source collection property maps to an empty destination collection, not null. So you can usually iterate the destination without a null check. When you map into an existing collection, AutoMapper clears it first, so existing items are lost. If you need to merge instead, look at AutoMapper.Collection.</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>
