2017-09-30 13 views
-1

에서 일부 개체를 제외 I 다음지도 규칙이 있습니다Automapper는 - 매핑 컬렉션을

CreateMap<ViewModels.ApplicationDriverAccidentFormVM, ApplicationDriverAccidentDomain>(); 

가 나는 모두가 사고의 특성을 가지고 있으며, ApplicationDriverDomain에 ViewModels.ApplicationDriverFormVM를 매핑 할 각 유형에 적합한 모음입니다.

public class ApplicationDriverDomain 
{ 
    public List<ApplicationDriverAccidentDomain> Accidents { get; set; } 
} 

public class ApplicationDriverFormVM 
{ 
    public List<ApplicationDriverAccidentFormVM> Accidents { get; set; } 
} 

그리고 (매핑되지 않음) 일부 조건 을 만족하지 않는 모든 레코드, 나는 다음과 같은 코드를 작성하려고 제외 할 :

 CreateMap<ViewModels.ApplicationDriverFormVM, ApplicationDriverDomain>() 
      .ForMember(dest => dest.Accidents, opt => opt.MapFrom(src => GetNotNullFromCollection(src.Accidents))) 

GetNotNullFromCollection은 다음과 같습니다

List<object> GetNotNullFromCollection(object input) 
    { 
     List<object> output = new List<object>(); 
     foreach (var item in (List<object>)input) 
     { 
      if (!Utils.IsAllNull(item)) 
       output.Add(item); 
     } 
     return output; 
    } 

하지만 내게 :

'System.Collections.Generic.List 1[Web.ViewModels.ApplicationDriverAccidentFormVM]' to type 'System.Collections.Generic.List 1 [System.Object]'형식의 개체를 캐스팅 할 수 없습니다.

왜, 어떻게해야합니까?

답변

0

나는 다음과 같은 방법으로 그것을 해결 :

CreateMap<ViewModels.ApplicationDriverFormVM, ApplicationDriverDomain>().ForMember(dest => dest.Accidents, opt => opt.MapFrom(src => src.Accidents.Where(o => !Utils.IsAllNull(o)))) 
0

귀하의 방법 GetNotNullFromCollection은 개체를 수신하지만 목록을 전달하고 있습니다. 어쨌든 객체 대신 Generics을 사용하는 것이 좋습니다.

+0

목록 입니다하지 객체? –

+0

목록 은 개체가 아닌 제네릭 형식의 목록입니다. 제네릭에 대한 자세한 내용을 읽어보십시오. – Yarimi