Máy tự động dễ dàng xử lý ánh xạ một danh sách các loại đối tượng sang danh sách các loại đối tượng khác nhau, nhưng có thể ánh xạ tới một danh sách hiện có bằng ID không?Danh sách ánh xạ vào danh sách hiện có trong Automapper bằng cách sử dụng phím
5
A
Trả lời
7
Tôi chưa tìm được cách nào tốt hơn những điều sau đây.
Đây là nguồn và đích.
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; }
}
Xác định trình chuyển đổi (Bạn nên thay đổi Danh sách <> thành bất kỳ loại nào bạn đang sử dụng).
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;
}
}
Và đây là cấu hình ánh xạ thực tế và ví dụ.
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);
Bạn sẽ nhận được kết quả như sau.
1
tôi tìm thấy bài viết này rất hữu ích và như vậy tôi nghĩ tôi sẽ phản hồi trong phiên bản generic của tôi về bộ chuyển đổi loại mà bạn có thể sử dụng để chọn tài sản để phù hợp trên từ từng đối tượng.
Sử dụng tất cả những gì bạn cần làm là:
// 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;
}
}
Các vấn đề liên quan
- 1. Automapper Danh sách copy vào danh sách
- 2. Danh sách Ánh xạ hai chiều
- 3. Chuyển đổi hoặc ánh xạ danh sách lớp thành danh sách lớp khác bằng cách sử dụng Lambda hoặc LINQ?
- 4. cách ánh xạ danh sách theo thứ tự trong nhibernate?
- 5. Làm thế nào để ánh xạ một List vào một SelectList trong ASP.NET MVC bằng cách sử dụng AutoMapper?
- 6. @OneToMany giới hạn kích thước danh sách ánh xạ
- 7. Làm cách nào để có thể chỉnh sửa danh sách nhiều danh sách bằng cách sử dụng asp.net mvc?
- 8. Ánh xạ một JSONObject vào một Danh sách với Flexjson trong Java/Android
- 9. Automapper với lồng danh sách con
- 10. cách ánh xạ một đối tượng ẩn danh tới một lớp bằng AutoMapper?
- 11. chắp thêm danh sách vào danh sách
- 12. AutoMapper ánh xạ lồng nhau phức tạp
- 13. Phụ thêm mục vào danh sách trong danh sách hiểu
- 14. Cách sử dụng "Chứa" trong Danh sách?
- 15. dereferencing danh sách bên trong danh sách bằng Python
- 16. Tìm kiếm bằng Python trong danh sách các danh sách
- 17. Automapper DynamicMap không lập bản đồ Danh sách các loại ẩn danh
- 18. Lấy danh sách các lĩnh vực với chú thích, bằng cách sử dụng phản ánh
- 19. Thêm danh sách vào danh sách các danh sách trong R
- 20. Union A Danh sách các Danh sách Sử dụng LINQ
- 21. Chèn một Danh sách vào Danh sách khác trong Redis
- 22. Thêm một danh sách vào danh sách khác trong java?
- 23. Phím tắt để thêm vào Danh sách trong HashMap
- 24. Scala - Hai danh sách vào danh sách Tuple
- 25. Cách lọc danh sách bằng cách sử dụng Predicate
- 26. Làm phẳng danh sách các chuỗi và danh sách các chuỗi và danh sách bằng Python
- 27. Cách thêm vào Danh sách khi sử dụng Đa luồng?
- 28. Chuyển đổi Danh sách vào một danh sách Pivot
- 29. Nhúng danh sách phát youtube bằng khay danh sách bên
- 30. R. cách tạo danh sách các danh sách trong R?