Unique values ​​of the old list

Possible duplicate:
Remove list from list

I have 2 lists:

var oldList = new List<int>(){1,2,3,4,5};

var newList = new List<int>(){1,2,3,6,7};

      

How can I get the unique values ​​of the old list (with LINQ)?

//hardcoded list:
var list =  new List<int>(){4,5};

//LINQ:
var list = ??

      

+3


source to share


5 answers


var unique = oldList.Except(newList);

      



http://msdn.microsoft.com/en-us/library/system.linq.enumerable.except.aspx

+5


source


var list = oldList.Except(newList).ToList();

      



This means that it will return all items in oldList

that are not in newList

.

+2


source


Do you mean values ​​in oldList that are not in newList? If yes:

var list = oldList.Except(newList);

      

See Enumerable.Except on MSDN.

Add a challenge ToList()

if you really want a list.

+2


source


You can use the method Enumerable.Except()

.

Produces multiple differences between two sequences.

var oldList = new List<int>() { 1, 2, 3, 4, 5 };
var newList = new List<int>() { 1, 2, 3, 6, 7 };

var value = oldList.Except(newList);

foreach (var i in value)
{
    Console.WriteLine(i);
}

      

Here DEMO

.

+2


source


You must use the method Except

.

var list = oldList.Except(newList);

      

Produces multiple differences between two sequences. [ Documentation ]

0


source







All Articles