public static ICollection<TFirst> Synchronize<TFirst, TSecond>( this ICollection<TFirst> first, IEnumerable<TSecond> second, Func<TSecond, TFirst> convert = null, Func<TFirst, int> firstHash = null, Func<TSecond, int> secondHash = null) { if (firstHash == null) { firstHash = x => x.GetHashCode(); } if (secondHash == null) { secondHash = x => x.GetHashCode(); } if (convert == null) { convert = x => Mapper.Map<TFirst>(x); } var firstCollection = first.ToDictionary(x => firstHash(x), x => x); var secondCollection = second.ToDictionary(x => secondHash(x), x => x); var toAdd = secondCollection.Where(item => firstCollection.All(x => x.Key != item.Key)).Select(x => convert(x.Value)); foreach (var item in toAdd) { first.Add(item); } var toRemove = firstCollection.Where(item => secondCollection.All(x => x.Key != item.Key)); foreach (var item in toRemove) { first.Remove(item.Value); } return first; } How can I fix the problem? Thanks, Matteo
> To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+unsubscribe@googlegroups.com.
> To post to this group, send email to nhu...@googlegroups.com.
> Visit this group at http://groups.google.com/group/nhusers.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
>
> --
> You received this message because you are subscribed to a topic in the Google Groups "nhusers" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/nhusers/OLXzSw0sU2s/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to nhusers+unsubscribe@googlegroups.com.
Thanks for the update - glad you've got it sorted :). Collection sync is always a pain…
I've just started testing a custom AutoMapper IObjectMapper implementation which does a full model collection <-- --> entity collection sync; if anyone's interested in giving feedback or improvements then drop me a note. It's currently at a very primitive stage, but works for basic scenarios and allows code such as
return this.WorkUnit.Execute( (session, context) => {
var entity = session.Get<Entity>(model.Id);
if(entity == null) context.Fail(…);
return context.Map(model, entity); // Entire object graph gets synced here
})
/Pete
> To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+u...@googlegroups.com.
> To post to this group, send email to nhu...@googlegroups.com.
> Visit this group at http://groups.google.com/group/nhusers.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
>
> --
> You received this message because you are subscribed to a topic in the Google Groups "nhusers" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/nhusers/OLXzSw0sU2s/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to nhusers+u...@googlegroups.com.
> To post to this group, send email to nhu...@googlegroups.com.
> Visit this group at http://groups.google.com/group/nhusers.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+u...@googlegroups.com.