protected void UpdateWithMergeConflicts(JToken offer, CouchDatabase dbOffers) { var result = dbOffers.UpdateAsync(offer).Result; if (result.StatusCode == System.Net.HttpStatusCode.Conflict) { var offerNewer = dbOffers.GetAsync(offer.GetString("_id")).Result.Json; var offerObje = offer.ToObject <Offer>(); var offerNewerObj = offer.ToObject <Offer>(); offerObje.ListProduct.ToList().ForEach(x => { if (offerNewerObj.ListProduct.FirstOrDefault(y => y.Guid == x.Guid) != null) { x.ListUserCoupon = x.ListUserCoupon.Concat(offerNewerObj.ListProduct.FirstOrDefault(y => y.Guid == x.Guid).ListUserCoupon) .GroupBy(y => y.UserEmail) .Select(y => y .OrderBy(z => z.IsDelivered) .ThenByDescending(z => z.DateChange) .First()).ToList(); } }); offerObje.ListProduct.Union(offerNewerObj.ListProduct).GroupBy(x => x.Guid).Select(x => x.First()).ToList(); offer = JToken.FromObject(offerObje); offer["_rev"] = offerNewer["_rev"]; UpdateWithMergeConflicts(offer, dbOffers); } }