에서 일부 개체를 제외 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]'형식의 개체를 캐스팅 할 수 없습니다.
왜, 어떻게해야합니까?
목록입니다하지 객체? –
목록은 개체가 아닌 제네릭 형식의 목록입니다. 제네릭에 대한 자세한 내용을 읽어보십시오. –
Yarimi