AutoMapper Sorting List - automapper

I have this mapping defined
Mapper.CreateMap<Telephone, TelephoneDTO>()
.ForMember(dto => dto.Extension, opt => opt.MapFrom(src => src.Extension))
.ForMember(dto => dto.Number, 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))

Related

AutoMapper and mapping property from entity framework dbcontext

I am a bit new to AutoMapper, I am trying to map a property where the values from from an ef db context.
How would I go about doing that?
CreateMap<User, UserDto>()
.ForMember(dest => dest.Username, opt => opt.MapFrom(src => src.Username))
.ForMember(dest => dest.NetworkId, opt => opt.MapFrom(src => src.NetworkId))
.ForMember(dest => dest.Network, /*NEED TO TAKE NetworkId and PERFORM A lookup in DbContext AND SET Network property*/);

AutoMapper specific value for a property

I'm trying to map List to List but one of the properties of the DtoLineItem is different.
How can I tell AutoMapper to map a specific property of LineItem to that Property?
I've tried this:
CreateMap<Cart, CartModel>()
.ForMember(dest => dest.LineItems, opt => opt.MapFrom(src => src.LineItems))
.ForMember(dest => dest.LineItems.Select(x => x.CustomField),
opt => opt.MapFrom(src => src.LineItems.Select(y => y.Description)));
AutoMapper.AutoMapperConfigurationException Custom configuration for
members is only supported for top-level individual members on a type.

Automapper - Mapping Multiple Properties using 1 Resolver

Mapper.CreateMap<WorkItemSummary, WorkItemSummaryDto> ()
.ForMember(dto => dto.ProductDisplayName, opt => opt.MapFrom(src => src.Product.DisplayName))
.ForMember(dto => dto.TeamId, opt => opt.MapFrom(src => src.Product.Team.TeamId))
.ForMember(dto => dto.TeamName, opt => opt.MapFrom(src => src.Product.Team.TeamName))
.ForMember(dto => dto.RRConsolidated, opt => opt.ResolveUsing<WIConsolidateResolveReasonResolver>())
.ForMember(dto => dto.NewBugsQuery, opt => opt.ResolveUsing<WIQueryNewBugsResolver>())
.ForMember(dto => dto.ResolvedBugsQuery, opt => opt.ResolveUsing<WIQueryResolvedResolver>())
.ForMember(dto => dto.ClosedBugsQuery, opt => opt.ResolveUsing<WIQueryClosedResolver>())
.ForMember(dto => dto.BacklogQuery, opt => opt.ResolveUsing<WIQueryBacklogResolver>())
.ForMember(dto => dto.ResolvedReasonFixQuery, opt => opt.ResolveUsing<WIQueryResolvedReasonFixResolver>());
--- Adding One Resolver Code and mostly all resolvers are doing same Job but pulling different content from Xml Nodes
public class WIQueryNewBugsResolver: ValueResolver<WorkItemSummary, string>
{
protected override string ResolveCore(WorkItemSummary source)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(System.IO.Path.Combine(AppDomain.CurrentDomain.RelativeSearchPath, "WIsQuery.xml"));
....
}
}
I have multiple resolvers doing almost same job of reading some xmls and getting the data. Thinking of consolidating all those calls under one resolver.
ex: WIConsolidateResolveReasonResolver, WIQueryResolvedReasonFixResolver, WIQueryBacklogResolver
In this regard, can I call one resolver once and get all the other properties populated in one call.
Is there any other method call in place of ForMember
Thanks
use "forallmembers" method of automapper if same job you are doing.may be it will help for you.

Automapper ConfigurationExpression Condition

Quick question - whats the difference in the following?
This one works:
CreateMap<OrderResult, OrderViewModel>()
.ForMember(x => x.SoldTo, opt => opt.Ignore())
.ForMember(x => x.ShipTo, opt => opt.Ignore())
.ForMember(x => x.ShowPlaceOrder, opt => opt.MapFrom(c => c.Messages.Count == 0));
I would expect this one to do pretty much the same thing except "Mapper.AssertConfigurationIsValid();" fails on this one saying that "ShowPlaceOrder" is not mapped.
CreateMap<OrderResult, OrderViewModel>()
.ForMember(x => x.SoldTo, opt => opt.Ignore())
.ForMember(x => x.ShipTo, opt => opt.Ignore())
.ForMember(x => x.ShowPlaceOrder, opt => opt.Condition(c => c.Messages.Count == 0));
Thanks
Joe
You still need to provide a source for ShowPlaceOrder in case the condition is true. The "Condition" method takes a predicate to decide if the mapping should be done. I think your first example is more clear.

Issue with Ignoring nested Properties using Automapper

I have ran into an issue where I am trying to ignore properties within properties. e.g.
Mapper.CreateMap<Node, NodeDto>()
.ForMember(dest => dest.ChildNodes, opt => opt.Ignore())
.ForMember(dest => dest.NodeType.EntityType.Properties, opt => opt.Ignore());
I get following exception:
{"Expression 'dest => dest.NodeType.EntityType.Properties' must resolve to top-level member.\r\nParameter name: lambdaExpression"}
Any idea?
Well I have managed to figure it out by myself. I have to specify the nested property options in its own dto mapping. However let me know if there is another better way of doing this
Mapper.CreateMap<EntityType, EntityTypeDto>()
.ForMember(dest => dest.Properties, opt => opt.Ignore());
Mapper.CreateMap<Node, NodeDto>()
.ForMember(dest => dest.ChildNodes, opt => opt.Ignore());

Resources