【发布时间】: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