Dumpen
Dumpen

Reputation: 1672

Moving a list of ints to the front of a list

I have a List of Int64 (List A) that needs to moved infront of another List (Int64) (List B).

List B will ALWAYS contain the numbers from List A.

So say the List A has the following numbers:

1, 4, 5

List B could then look something like this:

1, 9, 5, 2, 10, 15, 4

The end result should then look like this:

1, 4, 5, 9, 2, 10, 15

What is the easiest way of moving the numbers from the first list to the front of the second list?

I thought about removing all List A numbers from List B and then adding them to front again, but I can't seem to grasp my head around the programming itself.

Upvotes: 4

Views: 73

Answers (1)

Wouter de Kort
Wouter de Kort

Reputation: 39898

Yo can try the following:

var result = listA.Concat(listB.Except(listA)).ToList();
// Gives: 1, 4, 5, 9, 2, 10, 15, 14

Except removes all elements of listA from listB. Concat then adds them to the front of the list.

Upvotes: 8

Related Questions