I am new to ABP and relatively new to AutoMapper, so I am a bit stumped about why some code is throwing an exception. I am trying to have one of my AppService methods return a Dto , but it throws an exception when I call ObjectMapper.Map().
The exception is : AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.
But since the "schools" variable is an anonymous type, I can't setup a mapping configuration, right? The Dto is super simple, just one field. I could eliminate the use of AutoMapper, but I would like to be consistent and use it in all cases just like the existing AppServices.
namespace MyProject.Organizations.Dto
{
public class SchoolSearchDto
{
public string DisplayName { get; set; }
}
}
The AppService method:
public async Task<ListResultDto<SchoolSearchDto>> SchoolSearch(SchoolSearchInput input)
{
var query = from uou in _extendedOrganizationUnitRepository.GetAll()
where uou.DisplayName.Contains(input.SchoolName)
select new { uou.DisplayName, uou.ParentId };
var schools = await query.WhereIf(input.ParentId != null, p => p.ParentId == input.ParentId).ToListAsync();
return new ListResultDto<SchoolSearchDto>(
ObjectMapper.Map<List<SchoolSearchDto>>(schools)
);
}
I also tried using this return statement:
return new ListResultDto<SchoolSearchDto>(
schools.Select(school =>
{
var dto = ObjectMapper.Map<SchoolSearchDto>(school.ou);
return dto;
}).ToList());
3 Answer(s)
-
0
Hi,
You are right, you cannot use mapping for anonymous types. You can create a new dto and select it here like this
select new MyCustomDto { uou.DisplayName, uou.ParentId }
Or as I can see, you can simply write your code like this without using anonymous types.
var schools = _extendedOrganizationUnitRepository.GetAll() .Where(ou => uou.DisplayName.Contains(input.SchoolName)) .WhereIf(p => input.ParentId != null, p => p.ParentId == input.ParentId) .ToListAsync(); return new ListResultDto<SchoolSearchDto>( ObjectMapper.Map<List<SchoolSearchDto>>(schools) );
Thanks.
-
0
Thanks. I used your first suggestion. I wasn't able to use the second approach because I had to add a reference to the parent OU in my query and it appears that there is no parent navigation property built into AbpOU, so I couldn't use Include().
Thanks again, Bryan
-
0
good to hear it works ;)