来自另一个不同类型的内部列表的 C# Automapper 内部列表
C# Automapper inner list from another inner list of different type
我正在尝试映射类似于此模型的内容:
class Source {
...
SubSource subSource;
}
class SubSource {
...
List<SourceListItem> list;
SomeInfo someInfo;
...
}
class SomeInfo {
string name;
...
}
class SourceModel {
...
SomeInfoModel someInfoModel;
...
}
class SomeInfoModel {
string name;
List<SourceListItemModel> list;
...
}
我需要的是将"SubSource.List"映射到"SomeInfoModel.List"。我能够正确映射每个 属性,但映射后列表始终为空,并且在执行过程中没有发生错误。
我有以下映射配置:
CreateMap<SourceListItem, SourceListItemModel>()
CreateMap<SomeInfo, SomeInfoModel>()
CreateMap<Source, SourceModel>()
...
.ForPath(dest => dest.someInfoModel.list, opt => opt.MapFrom(src =>
src.subSource.list))
...
执行您的代码时,对 configuration.AssertConfigurationIsValid();
的调用会抛出一个异常,其中清楚地描述了您 运行 遇到的问题
Unmapped members were found. Review the types and members below. Add a
custom mapping expression, ignore, add a custom resolver, or modify
the source/destination type.
For no matching constructor, add a no-arg ctor, add optional
arguments, or map all of the constructor parameters
SomeInfo -> SomeInfoModel (Destination member list) SomeInfo ->
SomeInfoModel (Destination member list)
Unmapped properties: list
试图重现,但对我来说 list
属性 的映射有效。
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<SourceListItem, SourceListItemModel>();
cfg.CreateMap<SomeInfo, SomeInfoModel>();
cfg.CreateMap<Source, SourceModel>()
.ForPath(dest => dest.someInfoModel.list, opt => opt.MapFrom(src =>
src.subSource.list));
});
var source = new Source()
{
subSource = new SubSource()
{
list = new List<SourceListItem>()
{
new SourceListItem() { Text1 = "text1" },
new SourceListItem() { Text1 = "text2" },
}
}
};
var mapper = new Mapper(config);
var model = mapper.Map<SourceModel>(source);
使用的型号:
public class SourceListItem
{
public string Text1 { get; set; }
}
public class SourceListItemModel
{
public string Text1 { get; set; }
}
我正在尝试映射类似于此模型的内容:
class Source {
...
SubSource subSource;
}
class SubSource {
...
List<SourceListItem> list;
SomeInfo someInfo;
...
}
class SomeInfo {
string name;
...
}
class SourceModel {
...
SomeInfoModel someInfoModel;
...
}
class SomeInfoModel {
string name;
List<SourceListItemModel> list;
...
}
我需要的是将"SubSource.List"映射到"SomeInfoModel.List"。我能够正确映射每个 属性,但映射后列表始终为空,并且在执行过程中没有发生错误。
我有以下映射配置:
CreateMap<SourceListItem, SourceListItemModel>()
CreateMap<SomeInfo, SomeInfoModel>()
CreateMap<Source, SourceModel>()
...
.ForPath(dest => dest.someInfoModel.list, opt => opt.MapFrom(src =>
src.subSource.list))
...
执行您的代码时,对 configuration.AssertConfigurationIsValid();
的调用会抛出一个异常,其中清楚地描述了您 运行 遇到的问题
Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type.
For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters
SomeInfo -> SomeInfoModel (Destination member list) SomeInfo -> SomeInfoModel (Destination member list)
Unmapped properties: list
试图重现,但对我来说 list
属性 的映射有效。
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<SourceListItem, SourceListItemModel>();
cfg.CreateMap<SomeInfo, SomeInfoModel>();
cfg.CreateMap<Source, SourceModel>()
.ForPath(dest => dest.someInfoModel.list, opt => opt.MapFrom(src =>
src.subSource.list));
});
var source = new Source()
{
subSource = new SubSource()
{
list = new List<SourceListItem>()
{
new SourceListItem() { Text1 = "text1" },
new SourceListItem() { Text1 = "text2" },
}
}
};
var mapper = new Mapper(config);
var model = mapper.Map<SourceModel>(source);
使用的型号:
public class SourceListItem
{
public string Text1 { get; set; }
}
public class SourceListItemModel
{
public string Text1 { get; set; }
}