简体   繁体   中英

How to create expression with type cast for automapper to create map for Enum

I am having two classes :

source.Employee and destination.Employee .

I am getting just two name in my function destination property name ie destination.TestEnum1 and source property name ie source.TestEnum1.

I want to create expression dynamically as i mentioned below.

 var mapExpr = Mapper.CreateMap<Soure.Employee, destination.Employee>().ForMember(destination => destination.TestEnum1, opt => opt.MapFrom(source => (destination.MyEnum2)source.TestEnum1));

The expression is just

destination => destination.TestEnum1, opt => opt.MapFrom(source => (destination.MyEnum2)source.TestEnum1)

I am creating it to map Enum in Project().To();. As

Mapper.CreateMap<Soure.MyEnum1, destination.MyEnum2>() 

gives exception unable to map MyEnum2 to int 32.

Source employee :

namespace Soure
{
    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public Department dept1 { get; set; }

        public int age { get; set; }

        public MyEnum1 TestEnum1 { get; set; }
    }

    public enum MyEnum1
    {
        red = 1,
        yellow = 2
    }
}

destination employee class :

namespace destination
{
    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public int age { get; set; }
        public MyEnum2 TestEnum1 { get; set; }

        public Departments dept1 { get; set; }

    }
    public enum MyEnum2
    {
        red = 1,
        yellow = 2
    }
}

You might think of using Custom Type Converters for this, found here .

Here I made a sample application for you (simplified a bit) where I implemented a Enum1to2TypeConverter inheriting from the generic interface ITypeConverter :

public class Employee1
{
    public int Id { get; set; }
    public string Name { get; set; }
    public MyEnum1 TestEnum { get; set; }
}

public enum MyEnum1
{
    red = 1,
    yellow = 2
}

public class Employee2
{
    public int Id { get; set; }
    public string Name { get; set; }
    public MyEnum2 TestEnum { get; set; }
}

public enum MyEnum2
{
    red = 1,
    yellow = 2
}

public class Enum1to2TypeConverter : ITypeConverter<MyEnum1, MyEnum2>
{
    public MyEnum2 Convert(ResolutionContext context)
    {
        return (MyEnum2)(context.SourceValue);
    }
}

public class Test
{
    public void Example()
    {
        Mapper.CreateMap<MyEnum1, MyEnum2>().ConvertUsing(new Enum1to2TypeConverter());
        Mapper.CreateMap<Employee1, Employee2>();
        Mapper.AssertConfigurationIsValid();

        var source = new Employee1
        {
            Id = 1,
            Name = "Employee1-Name",
            TestEnum = MyEnum1.yellow
        };

        Employee2 result = Mapper.Map<Employee1, Employee2>(source);
        //Check content of result here!
    }
}

class Program
{
    private static void Main(string[] args)
    {
        (new Test()).Example();
    }
}

As the enums in your example share the same underlying int types, automapper will handle this automatically as follows:

    Mapper.CreateMap<Foo, Bar>()
    .ForMember(dest => dest.MyEnum2, opt => opt.MapFrom(src => src.MyEnum1))    
    ;

    var source = new Foo { MyEnum1 = MyEnum1.yellow };

    var destination = Mapper.Map<Bar>(source);

    Console.WriteLine(destination.MyEnum2);

Working Fiddle

You can check out ConvertProjectionUsing, which is the ConvertUsing for LINQ.

Mapper.CreateMap<MyEnum1, MyEnum2>().ProjectUsing(src => ???);

What should you put inside that ???, I'm not sure, it will depend on your query provider. EF may or may not be able to take that projection and convert it to SQL. You might be able to cast the source to int:

Mapper.CreateMap<MyEnum1, MyEnum2>().ProjectUsing(src => (MyEnum2)(int)src);

That is completely dependent on your query provider, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM