How can I compare two C# collections and issue Add, Delete commands to make them equal? -
i have 2 icollection collections:
public partial class objectivedetail { public int objectivedetailid { get; set; } public int number { get; set; } public string text { get; set; } } var _objdetail1: // contains list of objectivedetails database. var _objdetail2: // contains list of objectivedetails web front end.
how can iterate through these , issue , add, delete or update synchronize database latest web front end?
if there record present in first list not second to:
_uow.objectivedetails.delete(_objectivedetail);
if there record present in second list not first to:
_uow.objectivedetails.add(_objectivedetail);
if there record (same objectivedetailid) in first , second need see if same , if not issue an:
_uow.objectivedetails.update(_objectivedetail);
i thinking kind of:
foreach (var _objectivedetail in _objectivedetails) {}
but think might need have 2 of these , wondering if there better way. have suggestions how this?
the following code 1 of possible solutions
var tobeupdated = objectivedetail1.where( => objectivedetail2.any( b => (b.objectivedetailid == a.objectivedetailid) && (b.number != a.number || !b.text.equals(a.text)))); var tobeadded = objectivedetail1.where(a => objectivedetail2.all( b => b.objectivedetailid != a.objectivedetailid)); var tobedeleted = objectivedetail2.where(a => objectivedetail1.all( b => b.objectivedetailid != a.objectivedetailid));
the rest simple code add, delete, update 3 collections database.
Comments
Post a Comment