28

オートマッパーを使用して、ソース オブジェクトと宛先オブジェクトをマッピングしています。それらをマップしている間、以下のエラーが発生します。

式は最上位メンバーに解決される必要があります。パラメータ名: lambdaExpression

問題を解決できません。

ソース オブジェクトと宛先オブジェクトは次のとおりです。

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; }
        }
}

オブジェクトでマッピングSource.Carsする必要がありDestination.OutputData.Carsます。これで私を助けてもらえますか?

4

5 に答える 5

42

あなたが使用している:

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

destラムダで2レベルを使用しているため、これは機能しません。

Automapper では、1 つのレベルにしかマッピングできません。問題を解決するには、単一のレベルを使用する必要があります:

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

このようにして、車を目的地に設定できます。

于 2012-10-17T17:25:03.700 に答える
16
  1. Sourceとの間のマッピングを定義しOutputDataます。

    Mapper.CreateMap<Source, OutputData>();
    
  2. Destination.Outputでマップするように構成を更新しますOutputData

    Mapper.CreateMap<Source, Destination>().ForMember( dest => dest.Output, input => 
        input.MapFrom(s=>Mapper.Map<Source, OutputData>(s))); 
    
于 2012-07-25T10:31:49.073 に答える
14

あなたはそのようにすることができます:

// First: create mapping for the subtypes
Mapper.CreateMap<Source, OutputData>();

// Then: create the main mapping
Mapper.CreateMap<Source, Destination>().
    // chose the destination-property and map the source itself
    ForMember(dest => dest.Output, x => x.MapFrom(src => src)); 

それが私のやり方です;-)

于 2015-06-30T11:16:28.420 に答える