简体   繁体   中英

comparing two Lists in C#

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?

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);

Try this:

var result = list2.Except(list1);

Note that it's considered bad style to use initial capitals on your variable names (unless they're actually properties) :-)

Here you go: http://msdn.microsoft.com/en-us/library/bb300779.aspx

Rarest examples on msdn which i found useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM