2015-01-08 1 views
3
class Program 
    { 
     static void Main(string[] args) 
     { 
      var emp1 = new Soure.Employee(); 
      emp1.TestEnum = Soure.MyEnum1.red; 
      var emp2 = new Soure.Employee(); 
      emp2.TestEnum = Soure.MyEnum1.yellow; 
      var empList = new List<Soure.Employee>() { emp1, emp2 }.AsQueryable(); 
      var mapExpr = Mapper.CreateMap<Soure.Employee, destination.Employee>(); 
      Mapper.CreateMap<Soure.Department, destination.Department>(); 
      Mapper.CreateMap<Soure.MyEnum1, destination.MyEnum2>(); 
      Mapper.AssertConfigurationIsValid(); 
      var mappedEmp = empList.Project().To<destination.Employee>(); 

     } 
    } 

Source.Employee를 Destination.Employee에 매핑 중입니다. 모든 속성을 매핑 할 수 있습니다. 하지만 그것은 작동 그리고 재정을자동 매퍼에서 iqueryable 매핑에서 enum에 대한 맵을 만드는 방법은 무엇입니까?

var mapExpr = Mapper.CreateMap<Soure.Employee, destination.Employee>() 
     .ForMember(x => x.TestEnum, opt => opt.MapFrom(s => (destination.MyEnum2)s.TestEnum)); 

를 사용하는 경우 열거 매핑에 나에게 INT32 에 TestEnum를 매핑 할 수없는 예외를 제공합니다.

매핑 클래스는 노호 있습니다

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

     public Department dept { get; set; } 

     public int age { get; set; } 

     public MyEnum1 TestEnum { get; set; } 

     public Employee() 
     { 
      this.Id = 1; 
      this.Name = "Test Employee Name"; 
      this.age = 10; 
      this.dept = new Department(); 
     } 
    } 

    public class Department 
    { 
     public int Id { get; set; } 
     public string DeptName { get; set; } 

     Employee emp { get; set; } 

     public Department() 
     { 
      Id = 2; 
      DeptName = "Test Dept"; 
     } 
    } 

    public enum MyEnum1 
    { 
     red, 
     yellow 
    } 
} 

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

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

     public Department dept { get; set; } 

    } 

    public class Department 
    { 
     public int Id { get; set; } 
     public string DeptName { get; set; } 

     Employee emp { get; set; } 
    } 
    public enum MyEnum2 
    { 
     red, 
     yellow 
    } 
} 
+0

을 사용하는 것입니다

.ForMember(x => x.TestEnum, opt => opt.MapFrom(s => (int)s.TestEnum)); 

AutoMapper를 사용하여 매핑 열거의 2 가지 방법은 ... 다음 이 링크를 확인하십시오 : http://stackoverflow.com/questions/9721466/automapper-cannot-conve rt-enum-to-nullable-int – Mez

+0

그냥지도 만들기 사용하지 마십시오 ??? –

+0

ForMember에서 더 많은 정보를 지정해야하는 이유 as 다른 클래스가 있고 그 사이에 맵을 만들면 직접 매핑됩니다. –

답변

1

또 다른 방법은 ConstructUsing

mapper.CreateMap<TestEnum, Soure.MyEnum1>().ConstructUsing(dto => Enumeration.FromValue<Soure.MyEnum1>((int)dto.MyEnum2)); 
+0

그는 이미 그에게 말했습니다. 그는 우선 순위 (예 : ForMember)를 사용하고 싶지 않습니다. –

+0

지도 만들기 만 사용하면 안됩니까? –

+0

우리가 ForMember에서 더 많은 정보를 지정해야하는 이유 as 다른 클래스를 가지고 있고 그 사이에 맵을 만들면 직접 매핑합니다. –

관련 문제