using System.Linq; using System.Runtime.Serialization; namespace ShareeBike.Repository.Response { public class ComparableBikeDictionary : ComparableDictionary where S : BikeInfoBase { [OnDeserialized] internal void OnDeserializedMethod(StreamingContext context) { var toCorrectDictionary = this.Where( x => !string.IsNullOrEmpty(x.Value.bike) && x.Key != x.Value.bike).ToArray(); foreach (var element in toCorrectDictionary) { Remove(element.Key); if (ContainsKey(element.Value.bike)) { // Remove duplicates. Remove(element.Value.bike); continue; } Add(element.Value.bike, element.Value); } } /// /// Removes a bike from dictionary. /// /// Type of dictionary values to remove. /// If of bike to remove. /// True if at least one bike was removed, false if no bike was removed. public bool RemoveByBikeId( string bikeId) { if (string.IsNullOrEmpty(bikeId)) { // Nothing to do. return false; } var bikeToRemove = this.FirstOrDefault(x => bikeId == x.Value.bike); if (string.IsNullOrEmpty(bikeToRemove.Key)) { // Bike is not contained in dictionary. return false; } return Remove(bikeToRemove.Key); } /// /// Gets a bike by bike id from dictionary. /// /// Type of dictionary values to remove. /// If of bike to get. /// True if at least one bike was removed, false if no bike was removed. public S GetByBikeId( string bikeId) => this.FirstOrDefault(x => bikeId == x.Value.bike).Value; } }