간단한 DTO 객체 목록에서 변환해야하는 복잡한 객체가 있습니다.자동 완성 객체 및 복합 객체
내지도는 이것이다 :이 mapper.Map<IList<MyDto>>(my_singol_objA);
같은 singol와 ObjA 을 일하고 있어요하지만 난 mapper.Map<IList<MyDto>>(my_list_of_objA);
CreateMap<ObjASource, IEnumerable<MyDto>>()
.ConvertUsing(source => source.ObjB?.ObjC?.Select(p => new MyDto
{
field1 = source.field1,
field2 = source.field2,
field3 = source.field3,
field4 = p.fieldX,
field5 = p.fieldY
}).ToList()
);
이 문제를 해결하기 위해 추가해야하는 다른 종류의지도를 모르겠습니다. 내가
내 문제를보다 효율적이고 간단한 방법으로 설명 할 해결되지 않았기 때문에 편집을 할
고마워
는, 어쩌면 누군가가 나를 도울 수 있습니다. 컬렉션 안에 복잡한 개체가 있습니다. 이 같은
객체 다음 CompaniesProvideProduct 제품을 판매하고 자세한 내용은이 회사와의 관계를 포함
public class Product {
int product_id {get; set;};
string name {get; set;};
List<CompaniesProvideProduct> company {get; }
}
.
public class CompaniesProvideProduct {
int product_id {get; set;};
int company_id {get; set;};
decimal price {get; set;};
}
그런 다음 내 번역의 대상 개체는 다음과 같습니다 나는 목록 DB에서 얻을 내 프로그램에서 public class ProductDto {
int product_id {get; set;};
string name {get; set;};
int company_id {get; set;};
decimal price {get; set;};
}
나는 목록에서이 개체를 번역 할. 제품 레코드 1 개에 대해 ProductDto에 대한 추가 기록이 있습니다.
나는 시도했다 :CreateMap<Product, ProductDto>(); //to get product_id and name
CreateMap<CompaniesProvideProduct, ProductDto>(); //to get company_id and price
CreateMap<Product, IEnumerable<ProductDto>>()
.ConvertUsing<ProductConverter>();
public class ProductConverter : ITypeConverter<Product, IEnumerable<ProductDto>>
{
IEnumerable<ProductDto> ITypeConverter<Product, IEnumerable<ProductDto>>.Convert(Product source, IEnumerable<ProductDto> destination, ResolutionContext context)
{
Product prod = source;
foreach (var dto in prod.company.Select(e => context.Mapper.Map<ProductDto>(e)))
{
context.Mapper.Map(prod, dto);
yield return dto;
}
}
}
그러나 이것은 나를 위해 작동하지 않습니다. 누군가가 나를 도울 수 있으면 나는 매우 행복 할 것이다.
감사합니다.
컬렉션은 그들에게지도에 대한 필요가 없습니다, 기본적으로 처리됩니다. –
감사합니다 @ LucianBargaoanu 이걸 알고 있었지만 매핑이 잘못되어 오류가 나타납니다. –