I want to use automapper to map between 2 lists. One list contains a code and a string, the other contains the code and other fields. I want to use all the existing values/properties on the destination object, except for one. Is this possible with automapper? I don't want to specify all the properties with a UseDestinationValue(). Tried the following but instead of returning my destination list with the one property updated, it returns a new list with only matching items (in both lists) and all the fields are null:
public static IMappingExpression<TSource, TDest> UseAllDestinationValues<TSource, TDest>(this IMappingExpression<TSource, TDest> expression)
{
expression.ForAllMembers(opt => opt.UseDestinationValue());
return expression;
}
Mapper.CreateMap<Perm, PermViewModel>()
.UseAllDestinationValues()
.ForMember(dest => dest.CanEdit, opt => opt.MapFrom(s => s.editable));
var items = Mapper.Map<List<Perm>, List<PermViewModel>>(list, model.Items.Items);
I had to do something sort of similar using nullable doubles from one application to another. You could set up a type converter. Without your specific classes I can't speak to exactly how to do this, but here is my example.
Mapper.CreateMap<double?, double>().ConvertUsing<NullableDoubleConverter>();
private class NullableDoubleConverter : TypeConverter<double?, double>
{
protected override double ConvertCore(double? source)
{
if (source == null)
return Constants.NullDouble;
else
return (double)source;
}
}
You could obviously adapt this using something like.
Mapper.CreateMap<Perm, PermViewModel>().ConvertUsing<PermConverter>();
private class PermConverter : TypeConverter<Perm, PermViewModel>
{
protected override PermViewModel ConvertCore(Perm source)
{
return new PermViewModel()
{
//Set your parameters here.
};
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.