Reputation:
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
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.
Upvotes: 1
Views: 4091
Reputation: 1425
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
Upvotes: 0
Reputation: 14064
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);
Upvotes: 1