开发者

AutoMapper Sorting List

开发者 https://www.devze.com 2023-01-02 14:06 出处:网络
I have this mapping defined Mapper.CreateMap<Telephone, TelephoneDTO>() .ForMember(dto => dto.Extension, opt => opt.MapFrom(src => src.Extension))

I have this mapping defined

Mapper.CreateMap<Telephone, TelephoneDTO>()
.ForMember(dto => dto.Extension, opt => opt.MapFrom(src => src.Extension))
.ForMember(dto => dto.Number开发者_如何学C, opt => opt.MapFrom(src => src.Number))
.ForMember(dto => dto.Type, opt => opt.MapFrom(src => src.TelephoneType.Id));

when i do

IList<TelephoneDTO> dtos = Mapper.Map<IList<Telephone>, IList<TelephoneDTO>>(tels);

i would like the list of TelephoneDTO to be sorted by the Type.

How can i do that ?

thanks


AutoMapper is used for mapping, not for sorting. You could sort the list once the mapping being done:

IList<TelephoneDTO> dtos = Mapper
    .Map<IList<Telephone>, IList<TelephoneDTO>>(tels)
    .OrderBy(x => x.Type)
    .ToList();


or

IList<TelephoneDTO> dtos = Mapper
    .Map<IList<Telephone>, IList<TelephoneDTO>>(tels.OrderBy(x => x.Type))
0

精彩评论

暂无评论...
验证码 换一张
取 消