0

我试图使用 AutoMapper 将 DataServiceCollection 映射到字符串列表并创建反向映射。关于如何将这样的专业集合映射到另一个集合的任何想法?

Mapper.CreateMap<DataServiceCollection<LocationCountyValue>, List<string>>();
4

2 回答 2

1

您可以创建自定义类型转换器:

public class DataServiceCollectionToStringList : ITypeConverter<DataServiceCollection<LocationCountyValue>, List<string>> {
    public List<string> Convert(ResolutionContext context) {
        var sourceValue = (DataServiceCollection<LocationCountyValue>) context.SourceValue;

        /* Your custom mapping here. */
    }
}

然后使用以下命令创建地图ConvertUsing

Mapper.CreateMap<DataServiceCollection<LocationCountyValue>, List<string>>()
      .ConvertUsing<DataServiceCollectionToStringList>();
于 2015-06-14T03:08:54.607 回答
0

感谢 Thiago Sa,我创建了两个方向的映射,如下所示:

Mapper.CreateMap<DataServiceCollection<CountyValue>, List<string>>()
    .ConvertUsing((src) => { return src.Select(c => c.Value).ToList(); });

Mapper.CreateMap<List<string>, DataServiceCollection<CountyValue>>()
    .ConvertUsing((src) =>
    {
        return new DataServiceCollection<CountyValue>(
            src.Select(c => new CountyValue() { Value = c }));
    });
于 2015-06-15T12:54:04.423 回答