简体   繁体   English

Automapper 复杂映射从 List 到 Object

[英]Automapper Complex mapping from List to Object

I have object structure as below我有 object 结构如下

public class Source
{
    public Connection Connection { get; set;}
}

public class Connection
{
    public string Id { get; set;
    public IEnumerable<Distributor> Distributors { get; set;
}

public class Distributor { get; set; }
{
    public DistributorType Type {get; set;}
    public  string X { get; set; }
    public string Y { get; set; }
}

public class Destination
{
     public Distribution Distribution { get; set;
}

public class Distribution
{
    public string Id { get; set;}
    public  string X { get; set; }
    public string Y { get; set; }
}

I would like to map the Source to Destination for the property Distribution.我想将 map 的 Source to Destination 用于属性分布。 The mapping is as below映射如下

Source.Connection.Distributors.FirstOrDefault().X => Destination.Distribution.X
Source.Connection.Distributors.FirstOrDefault().Y => Destination.Distribution.Y
Source.Connection.Id => Destination.Distribution.Id 

I have tried using the Custom Resolver but no luck我曾尝试使用自定义解析器,但没有运气

public class CustomDistributorResolver : IValueResolver<Source, Destination, Distribution >
    {
        public Distribution  Resolve(Source source, Destination destination, Distribution destMember, ResolutionContext context)
        {
            var result = source.Connection.Distributors.FirstOrDefault(x => x.DistributorType =="ABC");
            if (result == null) return null;

            return new Distribution 
            {
                Id                      = source.Connection?.Id,
                X                       = result.X,
                Y                       = result.Y
            };
        }
    }

Mapping Pofile映射文件

CreateMap<Source, Destination>()
                .ForMember(d => d.Distribution, opt => opt.MapFrom( new CustomDistributorResolver()));

I always get Distribution value as NULL.我总是得到 NULL 的分布值。

I am not sure what I am doing wrong here on mapping.我不确定我在地图上做错了什么。

-Alan- -艾伦-

I am not very sure what you will deal with the IEnumerable.我不太确定您将如何处理 IEnumerable。 So I use linq to approach.所以我使用 linq 来接近。

var id = Source.Connection.Id;
var distributions = Source.Connection.Distributors.Select(m=> new Distribution()
                    {
                            Id = id,
                            X = m.X,
                            Y = m.Y,
                    });

Automapper would like: Automapper 想要:

CreateMap<Source, Destination>()
    .ForMember(dest => dest.Distribution.Id ,opt => opt.MapFrom(src => src.Connection.Id))
    .ForMember(dest => dest.Distribution.X,  opt => opt.MapFrom(src => src.Connection.Distributors.FirstOrDefault().X))
    .ForMember(dest => dest.Distribution.Y,  opt => opt.MapFrom(src => src.Connection.Distributors.FirstOrDefault().Y));

You can use a type converter您可以使用类型转换器

private class DestinationConverter : ITypeConverter<Source, Destination>
{
    public Destination Convert(Source source,
    Destination destination,

    ResolutionContext context)
    {
        var result = source.Connection.Distributors.FirstOrDefault(x => x.Type == "ABC");

        if (result == null) return null;
        destination = new Destination();

        destination.Distribution = new Distribution
        {
            Id = source.Connection?.Id,
            X = result.X,
            Y = result.Y
        };

        return destination;
    }
}

and register the converter并注册转换器

CreateMap<Source, Destination>().ConvertUsing<DestinationConverter>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM