0

AutoMapperを使用しています。私のソースオブジェクトは単純なクラスです

public class Source
    {

        public string FirstName { get; set; }

        public string type{ get; set; }
}

私の宛先は、typeという名前のオプション セットを含むMS Dynamics CRM Entity ( CrmSvctilを使用してモデルを生成しました)です。

以下は私のマッピングです

AutoMapper.Mapper.CreateMap<Source, Destination>()
               .ForMember(dest => dest.type, opt => opt.MapFrom(src => src.type)); 

タイプが一致しないというエラーが表示されます

基本的に私の問題は

AutoMapper を使用して文字列をオプション セット値にマップする方法がわかりません

4

1 に答える 1

0

OptionSet は、String ではなく Int 型の Value プロパティを持つ OptionSetValue として格納されるため、型の不一致エラーが発生します。

タイプが実際の int の場合は、それを解析するだけです。

AutoMapper.Mapper.CreateMap<Source, Destination>()
           .ForMember(dest => dest.type, opt => opt.MapFrom(src => new OptionSetValue(int.parse(src.type))));

ただし、それがオプション セットの実際のテキスト値である場合は、OptionSetMetaData を使用してテキスト値を検索する必要があります。

public OptionMetadataCollection GetOptionSetMetadata(IOrganizationService service, string entityLogicalName, string attributeName)
{
    var attributeRequest = new RetrieveAttributeRequest
    {
        EntityLogicalName = entityLogicalName,
        LogicalName = attributeName,
        RetrieveAsIfPublished = true
    };
    var response = (RetrieveAttributeResponse)service.Execute(attributeRequest);
    return ((EnumAttributeMetadata)response.AttributeMetadata).OptionSet.Options;
}

var data = GetOptionSetMetadata(service, "ENTITYNAME", "ATTRIBUTENAME");
AutoMapper.Mapper.CreateMap<Source, Destination>()
           .ForMember(dest => dest.type, opt => opt.MapFrom(src => new OptionSetValue(optionList.First(o => o.Label.UserLocalizedLabel.Label == src.type))));
于 2014-03-26T16:50:48.110 に答える