简体   繁体   English

AutoMapper-将整数映射到int数组

[英]AutoMapper - mapping integers to int array

I'm trying to map integers from a database to an int array, but I'm getting the error: AutoMapper.AutoMapperConfigurationException: 'Custom configuration for members is only supported for top level individual members on a type.' 我正在尝试将整数从数据库映射到int数组,但出现错误: AutoMapper.AutoMapperConfigurationException: 'Custom configuration for members is only supported for top level individual members on a type.'

This is what I have: 这就是我所拥有的:

Model: 模型:

public class Year
{
    public int[] Months { get; set; } = new int[3];
}

Mapping: 制图:

CreateMap<DataRow, Year>()
    .ForMember(dest => dest.Months[0], opt => opt.MapFrom(src => src["Jan"]))
    .ForMember(dest => dest.Months[1], opt => opt.MapFrom(src => src["Feb"]))
    .ForMember(dest => dest.Months[2], opt => opt.MapFrom(src => src["Mar"]))

Anyone know how to get this to work? 任何人都知道如何使它工作吗?

You will need to do something like this 您将需要执行以下操作

.ForMember(dest => dest.Months, opt => opt.MapFrom(src => MapFromRow(src)))

where you have a method 你有办法

int[] MapFromRow(DataRow src)
{
    int months = new int[12];

    months[0] = src["Jan"];
    ...
    return months;
}

AutoMapper does not seem to support "dotting" into a property or indexing into an array. AutoMapper似乎不支持在属性中添加点或在数组中建立索引。

If you want to go fully inline, you can do 如果您想完全内联,则可以

.ForMember(dest => dest.Months, opt => opt.MapFrom(src => new int[]
    {
        (int)src["Jan"], (int)src["Feb"], ...
    }))

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

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