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
Post a Comment