Note that there are some explanatory texts on larger screens.

plurals
  1. POAutomapper Projections and Enum > string converter
    primarykey
    data
    text
    <p>Having a problem using Automapper's <code>Project().To</code> method that does not show up when doing a simple <code>Mapper.Map&lt;&gt;()</code>.</p> <p>I have an enum that is mapped to a string, and a class containing that enum that is mapped to another class containing a string property with the same name as the enum property. </p> <p>When I do a simple <code>Mapper.Map&lt;&gt;()</code> from one class to another, everything works fine. But when I try to do a <code>Project().To()</code>, I get an exception:</p> <pre><code>System.ArgumentException: Type 'System.String' does not have a default construct or at System.Linq.Expressions.Expression.New(Type type) at AutoMapper.MappingEngine.CreateMapExpression(Type typeIn, Type typeOut) at AutoMapper.MappingEngine.CreateMapExpression(Type typeIn, Type typeOut) at AutoMapper.MappingEngine.&lt;CreateMapExpression&gt;b__9[TSource,TDestination](T ypePair tp) at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Fu nc`2 valueFactory) at AutoMapper.MappingEngine.CreateMapExpression[TSource,TDestination]() at AutoMapper.QueryableExtensions.ProjectionExpression`1.To[TResult]() at ConsoleApplication1.Program.Main(String[] args) </code></pre> <p>Here's a code sample that demonstrates the problem:</p> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using AutoMapper.QueryableExtensions; namespace ConsoleApplication1 { public static class EnumExtensions { public static string DisplayName(this Enum e) { var field = e.GetType().GetField(e.ToString()); if (field != null) { var display = ((DisplayAttribute[])field.GetCustomAttributes(typeof(DisplayAttribute), false)).FirstOrDefault(); if (display != null) { return display.Name; } } return e.ToString(); } } public enum Foo { [Display(Name = "Thing 1")] Thing1, [Display(Name = "Thing 2")] Thing2 } public class Bar { public Foo SomeFoo { get; set; } public string Name { get; set; } } public class BarViewModel { public string SomeFoo { get; set; } public string Name { get; set; } } class Program { static void Main(string[] args) { try { // map from enum value to enum display name AutoMapper.Mapper.CreateMap&lt;Foo, string&gt;().ConvertUsing(x =&gt; x.DisplayName()); AutoMapper.Mapper.CreateMap&lt;Bar, BarViewModel&gt;(); AutoMapper.Mapper.AssertConfigurationIsValid(); List&lt;Bar&gt; bars = new List&lt;Bar&gt;(); bars.Add(new Bar() { Name = "Name1", SomeFoo = Foo.Thing2 }); bars.Add(new Bar() { Name = "Name2", SomeFoo = Foo.Thing1 }); var barsQuery = (from Bar b in bars select b).AsQueryable(); // works exactly as expected var barViewModesls1 = AutoMapper.Mapper.Map&lt;IEnumerable&lt;BarViewModel&gt;&gt;(barsQuery).ToList(); // throws an exception "Type 'System.String' does not have a default constructor" var barViewModels2 = barsQuery.Project().To&lt;BarViewModel&gt;().ToList(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } Console.WriteLine("press a key to continue"); Console.ReadKey(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload