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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:31:38+00:00 2026-06-03T02:31:38+00:00

I have a common issue that I’d like to (hopefully) find a better solution

  • 0

I have a common issue that I’d like to (hopefully) find a better solution for moving forward. I have an ObservableCollection containing a master list of data. In my client code I need to ‘transform’ the data into a new form for display to the user. I use a LINQ statement like:

var newList = (from item in observable
               select new { FirstInitial = item.Name[0] });

I know it’s pretty rudimentary but it is enough to demonstrate the problem. (Notice the projection, this is not a simple filter or grouping statement.) I then display newList in my UI via data-binding.

Problem is, when the original collection changes, the UI doesn’t. The solution I’ve applied thus far has been to attach an event handler to the original collection’s CollectionChanged event which re-evaluates the query and updates the bound property.

While this works fine, it’s a lot of repeated code every time I run across this scenario. Is there a way that I can have the LINQ query return an ObservableCollection that “automatically” updates when the source Observable is changed?

In other words, I’d like to implement some ‘canned’ functionality so I can simply reuse it whenever I have this scenario.

UPDATE

Thanks to Scroog1 for helping me see my original post was too coupled to the UI for what I was really asking. Take the following example as a better description of the problem:

public class SomeClass
{
    private ObservableCollection<Employee> _allEmployees;
    private ObservableCollection<Employee> _currentEmployees;

    public ObservableCollection<Employee> CurrentEmployees
    {
        get
        {
            if (_currentEmployees == null)
                _currentEmployees = _allEmployees.Where(e => !e.IsTerminated);

            return _currentEmployees;
        }
    }
}

public class SomeViewModel
{
    private ICollectionView _view;

    public ICollectionView CurrentView
    {
        if (_view == null)
        {
            var cvs = new CollectionViewSource()
            {
                Source = someClass.CurrentEmployees
            }

            cvs.Add(new SortDescription("Name", ListSortDirection.Ascending));

            _view = cvs.View;
        }

        return _view;
    }
}

As you can see, the code where the query exists is not what is directly bound to the UI. I use this example to demonstrate that I am asking from a more general use-case than strictly a UI data-binding scenario.

  • 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-06-03T02:31:40+00:00Added an answer on June 3, 2026 at 2:31 am

    I would do something like this (assuming an ObservableCollection called observable and class implementing INotifyPropertyChanged with RaisePropertyChanged method):

    public IEnumerable<string> NewList
    {
        return from item in observable
               select item.Name;
    }
    
    observable.CollectionChanged += delegate { RaisePropertyChanged("NewList"); };
    

    Then the when observable is changed, the UI will be told that NewList has changed to and re-evaluate the query.

    For multiple dependent items you can do:

    observable.CollectionChanged += delegate
        {
            RaisePropertyChanged("NewList",
                                 "OtherProperty",
                                 "YetAnotherProperty",
                                 "Etc");
        };
    

    Update

    The above works fine in general for properties, as you will get the latest value every time you access it and INPC can be used to tell things to re-read it.

    For the slightly more interesting case of collections, I would implement a custom class that implements INotifyCollectionChanged and IEnumerable and wraps the LINQ. E.g.,

    public class CustomObservableCollection<T> : INotifyCollectionChanged,
                                                 INotifyPropertyChanged,
                                                 IEnumerable<T>
    {
        private readonly IEnumerable<T> _collection;
        public CustomObservableCollection(IEnumerable<T> collection)
        {
            _collection = collection;
        }
        public IEnumerator<T> GetEnumerator()
        {
            _collection.GetEnumerator();
        }
        public void RaiseCollectionChanged() { ... }
        ...
    }
    

    Then you can do:

    var newList = new CustomObservableCollection(from item in observable
                                                 select item.Name);
    observable.CollectionChanged += delegate { newList.RaiseCollectionChanged(); };
    

    Update 2

    You could even pass the dependency to CustomObservableCollection:

    public class CustomObservableCollection<T> : INotifyCollectionChanged,
                                                 INotifyPropertyChanged,
                                                 IEnumerable<T>
    {
        private readonly IEnumerable<T> _collection;
        public CustomObservableCollection(IEnumerable<T> collection,
                     params ObservableCollection[] dependencies)
        {
            _collection = collection;
            foreach (var dep in dependencies)
                dep.CollectionChanged += RaiseCollectionChanged();
        }
        public IEnumerator<T> GetEnumerator()
        {
            _collection.GetEnumerator();
        }
        public void RaiseCollectionChanged() { ... }
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue that I cannot find a solution for. I am developing
I have a pretty common layout issue that I have traditionally used a table
it seems common issue so I am surprised I didn't find solution already, maybe
I feel like this must be a common issue that I'm just struggling to
I find a common issue in my RESTful Rails apps controllers that respond to
A common issue I have is getting confused what $(this) is referring to. I
I have common functionality that I need to access from all screens of my
i have several common elements (components), that will generate some html. it seems my
We have a common problem of moving our development SQL 2005 database onto shared
I have a common comms library that i have written to communicate with our

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.