0

我有两节课:

source.Employee 和 destination.Employee 。

我的函数目标属性名称中只有两个名称,即destination.TestEnum1 和源属性名称,即source.TestEnum1。

我想动态创建表达式,如下所述。

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

表情简直了

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

我正在创建它以在 Project().To(); 中映射 Enum。作为

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

给出无法将 MyEnum2 映射到 int 32 的异常。

来源员工:

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
    }
}

目标员工类别:

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
    }
}
4

3 回答 3

0

您可以查看 ConvertProjectionUsing,它是 LINQ 的 ConvertUsing。

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

你应该在里面放什么???,我不确定,这取决于你的查询提供者。EF 可能会也可能不会进行该投影并将其转换为 SQL。您也许可以将源转换为 int:

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

不过,这完全取决于您的查询提供程序。

于 2015-01-13T01:10:40.823 回答
0

由于enums在您的示例中共享相同的基础int类型,automapper 将自动处理此问题,如下所示:

    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);

工作小提琴

于 2015-01-12T12:59:45.640 回答
0

您可能会考虑使用Custom Type Converters此功能,在此处找到。

在这里,我为您制作了一个示例应用程序(稍微简化了一点),其中我实现了Enum1to2TypeConverter从通用接口的继承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();
    }
}
于 2015-01-12T07:58:27.230 回答