Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...