Reputation: 21226
I have a collection of objects of type 'OrganisationUnit'
Id Index
A 1
B 2
C 3
D 4
E 5
F 6
G 7
Id F is moved before Id B
I have the both Id`s F and B as extra OrganisationUnit objects available passed from another layer.
What happens now:
The Id F gets the Index of Id B.
Id B and all following Id`s have to increment their Index by 1
The result should look like this:
Id Index
A 1
F 2
B 3
C 4
D 5
E 6
G 7
How would you do the merge the 2 objects with the collection (containing already those 2 objects) and do the updates with the index property with LINQ?
Upvotes: 0
Views: 195
Reputation: 460268
A little bit Linq and a simple loop:
var f = units.First(u => u.ID == "F");
int newFIndex = 2;
var updateUnits = units
.Where(u => u.Index >= newFIndex && u.Index < f.Index)
.ToList();
foreach (OrganisationUnit u in updateUnits)
u.Index++;
f.Index = newFIndex;
Upvotes: 1