オートマトンを使用して、次の値を実際のDateTimeオブジェクトに変換しようとしています。私はこの仕事をすることに失敗している、何かが間違っているのですか?任意の助け感謝オートマトンを使用して数値をDateTimeオブジェクトに変換する
例は、これまでAutoMapper
_mapperConfiguration = new MapperConfiguration(c => {
c.CreateMap<DataObject, DomainObject>()
.ForMember(dest => dest.DateTime, opt => opt.MapFrom(src => new DateTime(dest.SubString(0,4), dest.SubString(5,2), 1);
});
と
class DataObject
{
public int DateTime { get; set; }
}
class DomainObject
{
public DateTime DateTime { get; set; }
}
試みの下に示さ
201503
201504
201505
オブジェクトの値
コンパイラエラー
compiler error actually, I get the following Severity Code Description Project File Line Suppression State Error CS0103 The name 'dest' does not exist in the current context..
第二の試み
.ForMember(dest => dest.DateTime, opt => opt.MapFrom(src => new DateTime(src.Date.ToString().SubString(0, 4), src.Date.ToString().SubString(5, 2), 1)));
第三の試み
.ForMember(dest => dest.DateTime, opt => opt.MapFrom(src => new DateTime(Convert.ToInt32(src.Date.ToString().Substring(0, 4)), Convert.ToInt32(src.Date.ToString().Substring(5, 2)), 1)));
ソリューション
残念ながら、問題の原因となっていた悪いデータを取得していましたが、機能するオートマッパコードは以下のように閉鎖されています。
.ForMember(dest => dest.DateTime, dest => dest.MapFrom(src => DateTime.ParseExact(src.DateTime.ToString(), "yyyyMM", CultureInfo.InvariantCulture)));
2番目の試みははるかによく見えます。どのように失敗するか説明できますか? – Rawling
ありがとう! - それはコンパイルされますが、ランタイムエラーが発生します。インデックスと長さは、文字列内の場所を参照する必要があります。 – Jamie
サブストリングのインデックスが正しくありません。サブストリング(0,4)とサブストリング(4,2)でなければなりません。 – momar