public CollectionMappingFeature(IEquivalentComparer equivalentComparer) { EquivalentComparer = equivalentComparer; }
public static TDestination Map <TSource, TSourceItem, TDestination, TDestinationItem>(TSource source, TDestination destination, ResolutionContext context, IEquivalentComparer equivalentComparer) where TSource : IEnumerable <TSourceItem> where TDestination : ICollection <TDestinationItem> { if (source == null || destination == null) { return(destination); } var destList = destination.ToLookup(x => equivalentComparer.GetHashCode(x)).ToDictionary(x => x.Key, x => x.ToList()); var items = source.Select(x => { var sourceHash = equivalentComparer.GetHashCode(x); var item = default(TDestinationItem); List <TDestinationItem> itemList; if (destList.TryGetValue(sourceHash, out itemList)) { item = itemList.FirstOrDefault(dest => equivalentComparer.IsEquivalent(x, dest)); if (item != null) { itemList.Remove(item); } } return(new { SourceItem = x, DestinationItem = item }); }); foreach (var keypair in items) { if (keypair.DestinationItem == null) { destination.Add((TDestinationItem)context.Mapper.Map(keypair.SourceItem, null, typeof(TSourceItem), typeof(TDestinationItem))); } else { context.Mapper.Map(keypair.SourceItem, keypair.DestinationItem); } } foreach (var removedItem in destList.SelectMany(x => x.Value)) { destination.Remove(removedItem); } return(destination); }
static EquivalentExpression() { BadValue = new EquivalentExpression(); }
public static Expression <Func <TDestination, bool> > Map <TSource, TDestination>(TSource source, IEquivalentComparer <TSource, TDestination> toSourceExpression) { return(toSourceExpression.ToSingleSourceExpression(source)); }