Alan2
Alan2

Reputation: 24572

How can I compare two C# collections and issue Add, Delete commands to make them equal?

I have two ICollection collections:

public partial class ObjectiveDetail
{
    public int ObjectiveDetailId { get; set; }
    public int Number { get; set; }
    public string Text { get; set; }
}

var _objDetail1: // contains a list of ObjectiveDetails from my database.
var _objDetail2: // contains a list of ObjectiveDetails from web front end. 

How can I iterate through these and issue and Add, Delete or Update to synchronize the database with the latest from the web front end?

If there is a record present in the first list but not the second then I would like to:

_uow.ObjectiveDetails.Delete(_objectiveDetail);

If there is a record present in the second list but not the first then I would like to:

_uow.ObjectiveDetails.Add(_objectiveDetail);

If there is a record (same ObjectiveDetailId) in the first and second then I need to see if they are the same and if not issue an:

_uow.ObjectiveDetails.Update(_objectiveDetail);

I was thinking to do this with some kind of:

foreach (var _objectiveDetail in _objectiveDetails) {} 

but then I think I might need to have two of these and I am also wondering if there is a better way. Does anyone have any suggestions as to how I could do this?

Upvotes: 5

Views: 3011

Answers (3)

Abbas Amiri
Abbas Amiri

Reputation: 3204

The following code is one of some possible solutions

var toBeUpdated =
            objectiveDetail1.Where(
            a => 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 is a simple code to Add, Delete, Update the three collections to the database.

Upvotes: 5

Na Na
Na Na

Reputation: 838

foreach (var _objectiveDetail in _objectiveDetails) {} but then I think I might need to have two of these and I am also wondering if there is a better way. Does anyone have any suggestions as to how I could do this?

instead of looping through whole collection use LINQ query:

var query = from _objectiveDetail in _objectiveDetails

where (condition) select ... ;

update: It's pointless to iterate through whole collection if you want to update/delete/add something from web end. Humans are a bit slower than computers, isn't it? Do it one by one. In fact I don't understand the idea of 2 collections. What is it for? If you still want it: use event to run query, select updated/deleted/added record, do appropriate operation on it.

Upvotes: -1

Barak Liato
Barak Liato

Reputation: 11

It's look like you just want the two lists to be a copy of one another, you can just implement a Copy method and replace the outdated collection, if you implement ICollection you will need to implement CopyTo, also you can add a version field to the container so you can know if you need to update it. If you don't want to do it this way and you want to go through the elements and update them check if you can save in each object the state (modified, deleted, updated) this will help in the comparison.

Upvotes: 1

Related Questions