Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

comparing two Lists in C#

Tags:

c#

list

asp.net

I have two Lists, and I've values in these as,

List1                  List2
-----                  -----
 1                      1
 2                      2
                        3

I've to compare the second list with the first list, and I've to return the values which is not in the List1(here "3"), how can we compare two lists like this?

can anyone help me?

like image 529
shanish Avatar asked Nov 16 '25 20:11

shanish


1 Answers

Use LINQ and the Except extension method.

var list1 = new List<int> { 1, 2 };
var list2 = new List<int> { 1, 2, 3 };
var remaining = list2.Except(list1);
like image 172
Jon Avatar answered Nov 19 '25 08:11

Jon