I want a dynamic ignore for dateTime default value {1/1/0001 12:00:00 AM}
I used this code for nullable values
.ForMember(d => d.CreatedBy, opt => opt.Condition(s => !s.IsSourceValueNull))
but how may I do it for dateTime value
Try this :
.ForMember(dest => dest.CreatedOn,
opt => opt.Condition(src => src.CreatedOn != default(DateTime)))
Related
I have the following mapping for a property
.ForMember(x => x.Destination, opt => opt.MapFrom(x =>
x.Request.PurposeId == (int)PurposeTypes.PickUpShipment
? x.Request.RequestDestinations.FirstOrDefault().Destination
: x.Request.ShippingDestinationDetails.FirstOrDefault(dd => dd.ShippingAddressTypeId == (int)ShippingRequestAddressType.Destination).ShippingAddress.Destination
))
And the destination object map is like this
CreateMap<Destination, DestinationDTO>()
.ForMember(x => x.LocationCode, opt => opt.MapFrom(x => x.LocationCode))
.ForMember(x => x.CountryCode, opt => opt.MapFrom(x => x.CountryCode))
.ForMember(x => x.CountryName, opt => opt.MapFrom(x => x.State.StateShortName)); // this is not working
The problem is when I am using ProjectTo the CountryName is not loading because it is not loading the navigation property State of the destination, so x.State.StateShortName is coming empty.
And I found out the reason behind it, this is happening because I am loading the destination conditionally and because of this it is not loading the navigation property, because if I remove the condition and loads the destination like this it works
.ForMember(x => x.Destination, opt => opt.MapFrom(x =>
x.Request.RequestDestinations.FirstOrDefault().Destination
))
So basically ProjectTo is not loading the navigation property if the property mapping is conditional and not direct.
Is there any way I can achieve this?
Automapper version 8.0.0 removed ResolveUsing extension method from IMappingExpression and consolidated it with MapFrom extension method. However, after replacing ResolveUsing with MapFrom method, certain configurations throw exception.
Original ResolveUsing:
CreateMap<SourceType, DestinationType>()
.ForMember(dest => dest.Customer,
opt => opt.ResolveUsing(src => src?.Customer ?? new Customer())
);
Replaced with MapFrom:
CreateMap<SourceType, DestinationType>()
.ForMember(dest => dest.Customer,
opt => opt.MapFrom(src => src?.Customer ?? new Customer())
);
This produces compilation error:
Error CS8072
Automapper
An expression tree lambda may not contain a null propagating operator.
New Func-based overloads in Automapper 8.0.0 accept more parameters compared to old/removed ResolveUsing overloads.
Instead of using lambda expression with single input parameter opt.MapFrom(src => ...) when replacing ResolveUsing, overload with 2 parameters should be used opt.MapFrom((src, dest) => ...).
MapFrom expression becomes:
opt => opt.MapFrom((src, dest) => src?.Customer ?? new Customer())
Full example:
CreateMap<SourceType, DestinationType>()
.ForMember(dest => dest.Customer,
opt => opt.MapFrom((src, dest) => src?.Customer ?? new Customer())
);
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.
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.
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))