user3721173
user3721173

Reputation: 41

How to compare two list,ASP.net

I have two list and wanna know which items are in the fist one and arn't in second one,

List<int> viewphonenumid = new List<int>();
List<int?> DtoPhonenumId = new List<int?>();

For instance viewphonenumid has {1,2,3,4,5,6} and DtoPhonenumId has{3,4,5,6} and I wanna have {1,2} in a new list.

Upvotes: 0

Views: 1705

Answers (4)

HaMi
HaMi

Reputation: 539

try following:

List<int> result = new List<int>();
foreach(int element in viewphonenumid)
    if(!DtoPhonenumId.Contains(element))
        result.Add(element);

I hope that help

Upvotes: 0

iShurf
iShurf

Reputation: 31

var newList= viewphonenumid.Except(DtoPhonenumId).ToList();

You also can specify comparer.

http://msdn.microsoft.com/library/bb336390(v=vs.110).aspx

Upvotes: 1

Vladmir
Vladmir

Reputation: 1265

I suggest you to use Enumerable.Except http://msdn.microsoft.com/ru-ru/library/bb300779(v=vs.110).aspx

Something like this

(new List<int> {1,2,3,4}).Except(new List<int> {3,4})

Upvotes: 0

Arion
Arion

Reputation: 31239

Maybe something like this:

var newList= viewphonenumid.Where(v =>! DtoPhonenumId.Contains(v)).ToList();

Upvotes: 0

Related Questions