【问题标题】:Mapster and list to dictionary System.ArgumentNullException: 'Value cannot be null. Parameter name: key'Mapster 和字典列表 System.ArgumentNullException: '值不能为空。参数名称:key'
【发布时间】:2021-07-01 15:15:33
【问题描述】:

我有一个问题,我的 List 到 Dictionary 的映射不会将 Name 映射到 Key 属性。价值做得很好。实现如下。 System.ArgumentNullException: '值不能为空。 参数名称:key'

实施

_mapper.Map<List<MetaModel>, Dictionary<string, object>>(model.MemberPaymentVendor.Meta)

配置

config.NewConfig<MetaModel, KeyValuePair<string, object>>()
                .ConstructUsing(x => new KeyValuePair<string, object>(x.Name, x.Value));

       config.NewConfig<List<MetaModel>, Dictionary<string, object>>()
                 .MapWith(s => ToDictionary<MetaModel>(s));
    
public static Dictionary<string, object> ToDictionary<T>(List<T> source)
    {
        if (source == null)
            return new Dictionary<string, object>();

        return source.Select(v => v.Adapt<KeyValuePair<string, object>>()).ToDictionary(k => k.Key, v => v.Value);
    }

【问题讨论】:

    标签: dictionary mapster


    【解决方案1】:

    当我试图弄清楚它的原因时,我通过以下简单的例子抓住了它。

    var x = new KeyValuePair<string, object>();
    var y = new List<KeyValuePair<string, object>>();
    y.Add(x);
    y.ToDictionary(k => k.Key, v => v.Value); // <--- Value cannot be null. (Parameter 'key')
    

    你看到这里会发生什么吗?

    return source.Select(v => v.Adapt<KeyValuePair<string, object>>()).ToDictionary(k => k.Key, v => v.Value);
    

    【讨论】:

    • 我不知道你在说什么。 Mapster 和 Adapt 存在问题,无法翻译密钥。
    • 可能很清楚,但与我的问题无关。
    • @MikeFlynn 我建议你学习 C# Linq。
    • 这与 C# Linq 无关,而是 Mapster 及其映射方式。
    猜你喜欢
    • 2019-08-17
    • 1970-01-01
    • 2018-06-03
    • 2010-11-11
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 2015-11-05
    相关资源
    最近更新 更多