Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
637 views
in Technique[技术] by (71.8m points)

.net - Automapper expression must resolve to top-level member

I am using automapper to map source and destination objects. While I map them I get the below error.

Expression must resolve to top-level member. Parameter name: lambdaExpression

I am not able resolve the issue.

My source and destination objects are:

public partial class Source
{
        private Car[] cars;

        public Car[] Cars
        {
            get { return this.cars; }
            set { this.cars = value; }
        }
}

public partial class Destination
{
        private OutputData output;

        public OutputData Output
        {            
            get {  return this.output; }
            set {  this.output= value; }
        }
}

public class OutputData
{
        private List<Cars> cars;

        public Car[] Cars
        {
            get { return this.cars; }
            set { this.cars = value; }
        }
}

I have to map Source.Cars with Destination.OutputData.Cars object. Could you please help me in this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are using :

 Mapper.CreateMap<Source, Destination>()
 .ForMember( dest => dest.OutputData.Cars, 
             input => input.MapFrom(i => i.Cars)); 

This won't work because you are using 2 level in the dest lambda.

With Automapper, you can only map to 1 level. To fix the problem you need to use a single level :

 Mapper.CreateMap<Source, Destination>()
 .ForMember( dest => dest.OutputData, 
             input => input.MapFrom(i => new OutputData{Cars=i.Cars})); 

This way, you can set your cars to the destination.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...