简体   繁体   English

AutoMapper map 从嵌套集合到另一个集合,其中元素需要来自源的值

[英]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?给定以下类和一些示例数据,有没有办法获取 Foo 的实例并创建一个 Bar 集合来获取 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...使用以下配置,我可以将 CreditsAndDebitsByYear 字典 map 转换为“Bars”集合,但我想让 Bar.FooId 填充来自 dGrohl 的 Id 值,并且我一生都无法弄清楚该怎么做。 .

    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:根据@LucianBargaoanu 的评论,我可以通过执行以下操作来使其工作:

// 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);

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

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