简体   繁体   中英

AutoMapper map from nested collection to a different collection where elements need value from source

Given the following classes and some example data, is there a way to take an instance of Foo and create a collection of Bar which would grab Foo.Id?

public class CreditsAndDebits
{
    public decimal Credits { get; set; }
    public decimal Debits { get; set; }
}

public class Foo
{
    public int Id { get; set; }
    public Dictionary<int, CreditsAndDebits> CreditsAndDebitsByYear { get; set; }
}

public class Bar
{
    public int FooId { get; set; }
    public int Year { get; set; }
    public decimal Increase { get; set; }
    public decimal Decrease { get; set; }
}

Example data:

    var dGrohl = new Foo
    {
        Id = 13,
        CreditsAndDebitsByYear = new Dictionary<int, CreditsAndDebits>() {
        { 2019, new CreditsAndDebits { Credits = 100, Debits = 40 } } ,
        { 2020, new CreditsAndDebits { Credits = 80, Debits = 20 } } }
    };

Using the following configuration, I can map the CreditsAndDebitsByYear dictionary to a collection of "Bars" but I want to have Bar.FooId populated with the Id value from dGrohl and can't for the life of me figure out how to do it...

    var config = new AutoMapper.MapperConfiguration(cfg =>
    {
        cfg.CreateMap<KeyValuePair<int, CreditsAndDebits>, Bar>()
            .ForMember(dest => dest.Year, opt => opt.MapFrom(src => src.Key))
            .ForMember(dest => dest.Increase, opt => opt.MapFrom(src => src.Value.Credits))
            .ForMember(dest => dest.Decrease, opt => opt.MapFrom(src => src.Value.Debits));
    });

    var mapper = new AutoMapper.Mapper(config);

    var bars = mapper.Map<IEnumerable<KeyValuePair<int, CreditsAndDebits>>, IEnumerable<Bar>>(dGrohl.CreditsAndDebitsByYear);

在此处输入图像描述

Based on @LucianBargaoanu's comment, I was able to get this to work by doing the following:

// Not necessary but for consistency
public const string FOO_ID_KEY = "FOO_ID_KEY";

    var dGrohl = new Foo
    {
        Id = 13,
        CreditsAndDebitsByYear = new Dictionary<int, CreditsAndDebits>() {
        { 2019, new CreditsAndDebits { Credits = 100, Debits = 40 } } ,
        { 2020, new CreditsAndDebits { Credits = 80, Debits = 20 } } }
    };

    var config = new AutoMapper.MapperConfiguration(cfg =>
    {
        cfg.CreateMap<KeyValuePair<int, CreditsAndDebits>, Bar>()
            // Added following line:
            .ForMember(dest => dest.FooId, opt => opt.MapFrom((src, dest, destMember, context) => context.Items[FOO_ID_KEY]))
            .ForMember(dest => dest.Year, opt => opt.MapFrom(src => src.Key))
            .ForMember(dest => dest.Increase, opt => opt.MapFrom(src => src.Value.Credits))
            .ForMember(dest => dest.Decrease, opt => opt.MapFrom(src => src.Value.Debits));
    });

    var mapper = new AutoMapper.Mapper(config);

    // added the opt=>opt.Items part below
    var bars = mapper.Map<IEnumerable<KeyValuePair<int, CreditsAndDebits>>, IEnumerable<Bar>>(dGrohl.CreditsAndDebitsByYear, opt=>opt.Items[FOO_ID_KEY] = dGrohl.Id);

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.

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