1

I am trying to create a list of the Components running on the network. I am trying to get all the components in the ObservableCollection. ObservableCollection<ClsComponent> Now my question is if one of the component in the collection get changed / modified how would I be able to get it reflected to my ObservableCollection of Component

  1. Is there a way to change the it directly in the collection itself?
  2. What is the fast and efficient way doing it?

I have tried: to change it using the LINQ : Find the Component in the collection and change it?

var CompFound = Components.FirstOrDefault(x=>x.Id == myId);
Components.Remove(CompFound);
Components.Add(UpdatedComp);

I am very sure there should have been more optimized way of doing this. Please suggest.

Edit

I am trying to write the code in the function where I can get the parameters of Source Component and Destination Component. Function looks like this

public void UpdateComponent(ClsComponent SourceComp, ClsComponent DestComp)
{
  //do something here 
}

After the execution of the function I want to Replace Source Component with Destination Component.

linuxqwerty
  • 198
  • 16
  • You mean to say you wanted to change the Collection Directly? – Mohit S Aug 11 '15 at 03:40
  • 1
    Yes @MohitShrivastava I want to change it directly like in case of remove. Just need to call Collection.Remove(Obj) something of that sort –  Aug 11 '15 at 03:41
  • What you're doing there is replacing an existing item with a new one and not at the same index. Are you saying that you want to make changes to the existing item instead of doing the replacement? – jmcilhinney Aug 11 '15 at 03:48
  • 1
    By the way, you're misusing `FirstOrDefault`. – jmcilhinney Aug 11 '15 at 03:48
  • 2
    @jmcilhinney Yes both the statements are correct. I want to change an existing component but doing a replacement since I get the Updated Component in the form of Object of ClsComponent –  Aug 11 '15 at 03:54

2 Answers2

1

I believe this might work for you. I am sure you might be looking for this

Components.Insert(Components.IndexOf(SourceComp), DestComp);
Components.Remove(SourceComp);
Mohit S
  • 13,723
  • 6
  • 34
  • 69
0

One of the most efficient way would be to use a dictionary. There are implementations of ObservableDictionary which will give you the Observable behavior while allowing a fast key-based access to the object. Check this stackoverflow question. It includes Microsoft's

It should work like ObservableCollection, except it's also a dictionary. So you can Create ObservableDictionary<Int,ClsComponent>

To replace the value simply call

Components[myId] = destComp
Community
  • 1
  • 1
Tedy Pranolo
  • 1,285
  • 1
  • 14
  • 22
  • Nice link provided. Still in my case I dont want to use `INotifyCollectionChanged`. –  Aug 11 '15 at 04:19
  • Updated my answer. You don't need to use INotifyCollectionChanged to replace the value. A dictionary is also faster than indexOf if you have more than [3 items](http://www.dotnetperls.com/dictionary-time) – Tedy Pranolo Aug 11 '15 at 09:04