Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7411515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:23:54+00:00 2026-05-29T06:23:54+00:00

I have a dictionary with few pairs Dictionary<DateTime, ObservableCollection<Car>> and I’m changing year of

  • 0

I have a dictionary with few pairs

Dictionary<DateTime, ObservableCollection<Car>>

and I’m changing year of production of a Car, and because Key (DateTime) is a year I need to move this Car from KeyValuePair with old year to KeyValuePair with new year.

so for example, I had two pairs:

1.1.1997 {Car1,Car2}
1.1.1998 {Car3}

and by

Car2.Production = Convert.ToDateTime("1.1.1998");

I need to have

1.1.1997 {Car1}
1.1.1998 {Car3,Car2}

What is the easiest way to achieve that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:23:55+00:00Added an answer on May 29, 2026 at 6:23 am

    It seems like you might want to use a grouped view for this data instead, but here’s a code solution that addresses the question as stated. If you were planning on doing this for more than one property, I’d recommend making Car inherit from DependencyObject or implementing it from INotifyPropertyChanged instead of creating events for each.

    // The car class itself
    public class Car
    {
        // This event is raised when the production property changes
        public event EventHandler<PropertyValueChange<DateTime>> ProductionChanged;
        DateTime _production; // private data
        public DateTime Production
        {
            get { return _production; }
            set
            {
                if (value == _production) return; // don't raise the event if it didn't change
                var eventArgs = new PropertyValueChange<DateTime>(_production, value);
                _production = value;
                // If anyone is "listening," raise the event
                if (ProductionChanged != null)
                    ProductionChanged(this, eventArgs);
            }
        }
    }
    // Class that contains the dictionary of production to car lists
    class Foo
    {
        Dictionary<DateTime, ObservableCollection<Car>> ProductionToCars = new Dictionary<DateTime, ObservableCollection<Car>>();
    
        public void Add(Car c)
        {
            _Add(c);
            // We want to be notified when the car's production changes
            c.ProductionChanged += this.OnCarProductionChanged;
        }
        // This is called when a car's value changes, and moves the car
        void OnCarProductionChanged(object sender, PropertyValueChange<DateTime> e)
        {
            Car c = sender as Car;
            if (c == null) return;
            ProductionToCars[e.OldValue].Remove(c);
            _Add(c);
        }
        // this actually places the car in the (currently correct) group
        private void _Add(Car c)
        {
            ObservableCollection<Car> collection;
            // Find the collection for this car's year
            if (!ProductionToCars.TryGetValue(c.Production, out collection))
            {
                // if we couldn't find it, create it
                collection = new ObservableCollection<Car>();
                ProductionToCars.Add(c.Production, collection);
            }
            // Now place him in the correct collection
            collection.Add(c);
        }
    
    }
    // This class encapsulates the information we'll pass when the property value changes
    public class PropertyValueChange<T> : EventArgs
    {
        public T OldValue;
        public T NewValue;
        public PropertyValueChange(T oldValue, T newValue)
        {
            OldValue = oldValue;
            NewValue = newValue;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dictionary that I normally access with a key, so I need
I have a Dictionary with a few hundred thousand elements. I need to perform
Does VBA have dictionary structure? Like key<>value array?
I have a Python dictionary that labels key name to attributes. The program that
I need to read an XML file to a dictionary. I read few guides
I have a few objects in my class List<string> a List<string> b Dictionary<string,string> c
I have a Dictionary that for most operations I just need to retrieve a
I have a class that's currently inheriting from Dictionary and then adds a few
Let's say I have a dictionary with a few million words and phrases. For
I have a dictionary of 200,000 items (the keys are strings and the values

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.