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 6795533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:21:26+00:00 2026-05-26T18:21:26+00:00

To be more specific, let’s say we have a collection sorted by properties P1

  • 0

To be more specific, let’s say we have a collection sorted by properties P1 and P2 like this:

  • A 1
  • A 2
  • A 2
  • A 3
  • B 1
  • B 1
  • B 2
  • B 3

When doing a foreach through the collection, I want to get notified whenever a property changes.
So for P1 I need the following 4 notifications in this particular order:

  1. OnBeforeGroup(P1, “A”)
  2. OnAfterGroup(P1, “A”)
  3. OnBeforeGroup(P1, “B”)
  4. OnAfterGroup(P1, “B”)

For P2 this would make 12 notifications, spare me the list.

Of course, I can do this in every use case by saving the “old” property values within the foreach loop (OldP1 = P1) and firing an event/executing a method when P1 != OldP1, but that’s not reusable.

Is there a collection that already implements this pattern?
If not, any advice how to make it more reusable?

Edit: Note, I just want to get notified during foreach loop if some property value is different than it was in previous iteration, this has nothing to do with INotifyPropertyChanged etc.

  • 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-26T18:21:27+00:00Added an answer on May 26, 2026 at 6:21 pm

    .NET has ObservableCollection<T> but that’s used to watch changes happening to objects stored and collection itself. Your collection isn’t changing (given I understood you correctly) – you just want to get notified during loop if some property value is different than it was in previous iteration.

    Something that might point you in the right direction, in terms of having reusable code:

    public class PropertyWatcher<TSource>
    {
        // initialization code omitted
        private Dictionary<string, object> previousValues;
        private Dictionary<string, Func<TSource, object>> selectors; 
    
        public void RegisterWatcher<TSource>(Func<TSource, object> propertySelector, 
            string propertyName)
        {
            this.selectors.Add(propertyName, propertySelector);
            this.previousValues.Add(propertyName, null);
        }
    
        public void NotifyIfDifferent<TObject, TPropertyType>(TSource currentItem)
        {
            foreach (var key in this.selectors.Keys)
            {
                var selector = this.selectors[key];
                var currentValue = selector(currentItem);
                if (!currentValue.Equals(this.previousValues[key]))
                {
                    // raise notification, event; antyhing to your liking
                }
    
                this.previousValues[key] = currentValue;
            }
        }
    }
    

    And, assuming we’re dealing with P1 and P2 properties you mentioned, usage would look like this:

    var list = new List<SomeObject>(); // list of items you intend to watch
    var watcher = new PropertyWatcher<SomeObject>();
    watcher.RegisterWatcher((SomeObject o) => o.P1, "P1");
    watcher.RegisterWatcher((SomeObject o) => o.P2, "P2");
    foreach (var item in list)
    {
        watcher.NotifyIfDifferent(item);
    }
    

    This is by no means complete solution. It should work when simple values are concerned (so .Equals can handle comparing easily). Of course, you still need to add some event to PropertyWatcher class and perhaps tweak it here and there – but I think general idea is fairly easy to grasp.

    For more advanced scenarios though, I suppose you’ll have to work o more generic approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's imagine that I have something like this: <html> ... <div> <iframe src=test.html hash=r4d5f7></iframe>
I have asked aqbout timezones and date/time before but this is a more specific
Let me be more specific, it was hard to describe this in the title.
I have simple application. To be more specific this is Visual Studio Addin. Client
Let's say we have a structure like so: Try ' Outer try code, that
I do have something more specific in mind, however: Each web service method needs
I'm reviving this question, and making it more specific: Is there a .NET framework
I noticed This question , but my question is a bit more specific. Is
This is more a 'wonder why' than a specific issue but look at the
This is more of an algorithmic dilemma than a language-specific problem, but since I'm

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.