.net core 自动映射将嵌套对象集合展平为字符串集合
如何创建AutoMapper配置文件以映射多对多关系 我有三个实体-.net core 自动映射将嵌套对象集合展平为字符串集合,.net-core,automapper,.net Core,Automapper,如何创建AutoMapper配置文件以映射多对多关系 我有三个实体-Question、Tag和QuestionTag class Question { ICollection<QuestionTag> tags {get;set;} } class QuestionTag { int questionId {get;set;} Question question {get;set;} int tagId {get;set;} Tag tag {get
Question
、Tag
和QuestionTag
class Question
{
ICollection<QuestionTag> tags {get;set;}
}
class QuestionTag
{
int questionId {get;set;}
Question question {get;set;}
int tagId {get;set;}
Tag tag {get;set;}
}
class Tag {
string name {get;set;}
}
如何配置automapper配置文件以映射此内容
下面的映射导致了一个错误,可能是因为我无法深入到t.tag.name
CreateMap<Question, QuestionSummary>()
.ForMember(x => x.tags, o => o.MapFrom(x => x.tags.Select(t => t.tag.name)));
System.InvalidCastException: Unable to cast object of type 'System.Linq.Expressions.NewExpression' to type 'System.Linq.Expressions.MethodCallExpression'.
重做会有帮助的。制定一个我们可以执行并看到失败的计划。
CreateMap<Question, QuestionSummary>()
.ForMember(x => x.tags, o => o.MapFrom(x => x.tags.Select(t => t.tag.name)));
System.InvalidCastException: Unable to cast object of type 'System.Linq.Expressions.NewExpression' to type 'System.Linq.Expressions.MethodCallExpression'.
CreateMap<Tag, string>()
.ConvertUsing(x => x.name);
CreateMap<ICollection<Tag>, ICollection<string>>()
.ConvertUsing(x => x.Select(t => t.name).ToList());
CreateMap<Question, QuestionSummary>()
.ForMember(x => x.tags, o => o.MapFrom(x => x.tags.Select(t => t.tag)));