-1

I have an issue where my mapping for a List to Dictionary won't map the Name to the Key property. The Value does fine. The implementation is below. System.ArgumentNullException: 'Value cannot be null. Parameter name: key'

Implementation

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

Config

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);
    }
Mike Flynn
  • 22,342
  • 54
  • 182
  • 341

1 Answers1

0

When I tried to figure out its reason, I caught it with the following simple example.

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')

Did you see what can be incurred here?

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