オートマッパーはオブジェクトタイプのリストを異なるオブジェクトタイプの別のリストに簡単にマッピングできますが、IDをキーとして既存のリストにマップすることは可能ですか?リストをキーを使用してオートマッテの既存のリストにマップする
5
A
答えて
7
私は以下よりも良い方法を見つけられませんでした。
ここにソースと宛先があります。
public class Source
{
public int Id { get; set; }
public string Foo { get; set; }
}
public class Destination
{
public int Id { get; set; }
public string Foo { get; set; }
}
コンバータを定義してください(使用するタイプにリスト<を変更する必要があります)。
public class CollectionConverter: ITypeConverter<List<Source>, List<Destination>>
{
public List<Destination> Convert(ResolutionContext context)
{
var destinationCollection = (List<Destination>)context.DestinationValue;
if(destinationCollection == null)
destinationCollection = new List<Destination>();
var sourceCollection = (List<Source>)context.SourceValue;
foreach(var source in sourceCollection)
{
Destination matchedDestination = null;
foreach(var destination in destinationCollection)
{
if(destination.Id == source.Id)
{
Mapper.Map(source, destination);
matchedDestination = destination;
break;
}
}
if(matchedDestination == null)
destinationCollection.Add(Mapper.Map<Destination>(source));
}
return destinationCollection;
}
}
ここで実際のマッピングの設定と例を示します。
Mapper.CreateMap<Source,Destination>();
Mapper.CreateMap<List<Source>,List<Destination>>().ConvertUsing(new CollectionConverter());
var sourceCollection = new List<Source>
{
new Source{ Id = 1, Foo = "Match"},
new Source{ Id = 2, Foo = "DoesNotMatchWithDestination"}
};
var destinationCollection = new List<Destination>
{
new Destination{ Id = 1, Foo = "Match"},
new Destination{ Id = 3, Foo = "DoeNotMatchWithSource"}
};
var mergedCollection = Mapper.Map(sourceCollection, destinationCollection);
次の結果が得られます。
1
私はこの記事は非常に有用であることが判明、そのように私は私はあなたがそれぞれのオブジェクトからの一致するプロパティを選択するために使用できる型コンバータの私のジェネリック版にフィードバックするだろうと思いました。それあなたがする必要があるすべてを使用し
は次のとおりです。
// Example of usage
Mapper.CreateMap<UserModel, User>();
var converter = CollectionConverterWithIdentityMatching<UserModel, User>.Instance(model => model.Id, user => user.Id);
Mapper.CreateMap<List<UserModel>, List<User>>().ConvertUsing(converter);
//The actual converter
public class CollectionConverterWithIdentityMatching<TSource, TDestination> :
ITypeConverter<List<TSource>, List<TDestination>> where TDestination : class
{
private readonly Func<TSource, object> sourcePrimaryKeyExpression;
private readonly Func<TDestination, object> destinationPrimaryKeyExpression;
private CollectionConverterWithIdentityMatching(Expression<Func<TSource, object>> sourcePrimaryKey, Expression<Func<TDestination, object>> destinationPrimaryKey)
{
this.sourcePrimaryKeyExpression = sourcePrimaryKey.Compile();
this.destinationPrimaryKeyExpression = destinationPrimaryKey.Compile();
}
public static CollectionConverterWithIdentityMatching<TSource, TDestination>
Instance(Expression<Func<TSource, object>> sourcePrimaryKey, Expression<Func<TDestination, object>> destinationPrimaryKey)
{
return new CollectionConverterWithIdentityMatching<TSource, TDestination>(
sourcePrimaryKey, destinationPrimaryKey);
}
public List<TDestination> Convert(ResolutionContext context)
{
var destinationCollection = (List<TDestination>)context.DestinationValue ?? new List<TDestination>();
var sourceCollection = (List<TSource>)context.SourceValue;
foreach (var source in sourceCollection)
{
TDestination matchedDestination = default(TDestination);
foreach (var destination in destinationCollection)
{
var sourcePrimaryKey = GetPrimaryKey(source, this.sourcePrimaryKeyExpression);
var destinationPrimaryKey = GetPrimaryKey(destination, this.destinationPrimaryKeyExpression);
if (string.Equals(sourcePrimaryKey, destinationPrimaryKey, StringComparison.OrdinalIgnoreCase))
{
Mapper.Map(source, destination);
matchedDestination = destination;
break;
}
}
if (matchedDestination == null)
{
destinationCollection.Add(Mapper.Map<TDestination>(source));
}
}
return destinationCollection;
}
private string GetPrimaryKey<TObject>(object entity, Func<TObject, object> expression)
{
var tempId = expression.Invoke((TObject)entity);
var id = System.Convert.ToString(tempId);
return id;
}
}
関連する問題
- 1. マップの既存のキーにリストに値を追加する
- 2. 既存のリストを使用して新しいマップを作成
- 3. Pythonで既存のテキストファイルを使用してリストをフォーマットする
- 4. ラムダ式を使用して既存のリストに範囲を追加する
- 5. フィーチャレシーバと既存のリストテンプレートを使用してSharepointリストを作成するには
- 6. java 8リストのインスタンスを使用してマップするリストを変更する
- 7. 既存のツイッターユーザーをリストから削除する(Tweepyを使用)
- 8. AutoMapperを使用して辞書の値をリストにマップする
- 9. は、既存のリスト
- 10. は、既存のリスト
- 11. 複数のマップをマージしてリストに値を保存する
- 12. Pythonで既存のリストからリストを作成するには
- 13. ジェネレータを使用してリストをマップし、オブジェクトに変換する
- 14. Javaストリームを使用して、キーとそのキーの出現数を含むマップをリストから取得する
- 15. globを使用して既存のファイルパスのリストをフィルタリングできますか?
- 16. 反応キーを使用してリスト内のフォームデータにアクセスする
- 17. 複数の値リストを、Python辞書のキーのリストにマップしますか?
- 18. C#の既存のリストと比較してリストをソートする方法は?
- 19. s:iteratorを使用してマップのリストを反復する
- 20. AutoMapperを使用してリスト<object>をConfigurationElementCollectionにマップする
- 21. data.frameオブジェクトをrelistして既存のリストに追加します。
- 22. リストを<Object>にリスト<Map> RxJavaを使用して2つのキー
- 23. Jackson:リストの逆シリアル化を使用してマップするXML
- 24. jspを使用してGoogleマップのアドレスのリストを表す
- 25. 新しいリストインデックスを既存のリストに割り当てる
- 26. F#既存のリストに値を追加
- 27. Erlang:リストの理解を既存のリストに
- 28. Java8の複数のキーを使用したマップのリストの並べ替え
- 29. スカラーを使用してキー、リスト(値)をキー、値を使用して
- 30. ASP.Net Web APIを使用してキーのリストを送信し、キー/値のリストを返す方法?