オートマッパーはオブジェクトタイプのリストを異なるオブジェクトタイプの別のリストに簡単にマッピングできますが、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. フィーチャレシーバと既存のリストテンプレートを使用してSharepointリストを作成するには
- 3. java 8リストのインスタンスを使用してマップするリストを変更する
- 4. 複数の値リストを、Python辞書のキーのリストにマップしますか?
- 5. C#の既存のリストと比較してリストをソートする方法は?
- 6. s:iteratorを使用してマップのリストを反復する
- 7. Jackson:リストの逆シリアル化を使用してマップするXML
- 8. Erlang:リストの理解を既存のリストに
- 9. ASP.Net Web APIを使用してキーのリストを送信し、キー/値のリストを返す方法?
- 10. 辞書のキーとしてリストの値をキーとして使用
- 11. 辞書をPythonのリストのキーと値を使用して
- 12. jQuery.dataを使用して項目のリストを保存する
- 13. 各クロージャを使用してリストにマップを追加する際の問題
- 14. NSUserDefaultsを使用してCGPointのリストを保存します
- 15. lapplyを使用してリストのリストを操作する
- 16. DjangoマップされたリストとしてのGETキー
- 17. Pythonのdictの既存のリストからのdictのリスト
- 18. groovy:マップのキーから値を取得する(リストです)
- 19. Rのキーとして名前を使用リスト
- 20. Scheme内の既存のリストに要素を追加する
- 21. マップを使用して範囲からリストを作成
- 22. Rails association - 既存のリストからのカスタマイズされたリスト
- 23. Python:リストを使用して別々のリスト内のアイテムをチェックし、存在する場合は置換する
- 24. 選択リストの更新既存のオプション
- 25. Pythonの:多次元配列/リストにラムダなしでマップを()を使用して
- 26. 既存のUILocalNotificationsのリストを取得して日付を変更する
- 27. jQueryを使用して複数のリストを複数のリストに分割する
- 28. 使用されているすべての既知のターゲットトリプレットのリストは存在しますか?
- 29. Javaマップ/リストを使用した同様の構造のグループ
- 30. erlangリストを使用してリストを減算する:map