c# - How can I merge two Observable Collections, taking only the new and changed items from the new list, and add it to the old list? -


i have wpf app uses datagrid element bound observablecollection:

observablecollection<deployment> deployments = dataaccess.getdeployments(); datagrid01.itemssource = deployments; 

here deployment class. deployment object represents windows installation happening out in field, things currenttime changes frequently.

class deployment {     public string uniqueid { get; set; }             public string computername { get; set; }            public string ipaddress { get; set; }     public string make { get; set; }     public string model { get; set; }            public string username { get; set; }            public string tasksequencename { get; set; }           public string machineobjectou { get; set; }     public datetime? starttime { get; set; }     public datetime? currenttime { get; set; }     public string status { get; set; } } 

periodically want update datagrid changes these objects, currenttime or status. status changes 3 when finished installing, or 2 if in error state.

so, can new collection of objects on timer, best way add new items new collection datagrid, , change existing items have updated time, or status, or whatever?

a method shown below should job. not replace existing item in deployments collection, updates properties of item.

in order make update ui, deployment class have implement inotifypropertychanged interface , raise propertychanged when property value changes. simple solution add updatefrom(deployment d) method, updates properties , raises single propertychanged event null property name (to notify properties have changed).

using system.linq; ...  private void updatedeployments(ienumerable<deployment> changeddeployments) {     foreach (var changeddeployment in changeddeployments)     {         var deployment = deployments.firstordefault(             d => d.uniqueid == changeddeployment.uniqueid);          if (deployment != null)         {             deployment.updatefrom(changeddeployment);         }         else         {             deployments.add(changeddeployment);         }     } } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -