Elisabeth
Elisabeth

Reputation: 21206

Find count of an list2.item in list1

I have a list1 with these items:

"Test1"
"TestB"
"TestA"

and I have list2 with these items:

"Test1"
"Test2"
"Test3"
"Test4"
"Test5"

Case: list2.Test1 is the only item from list2 which occurs in list1, thats a positive case.

if list1 has ONE item of list2 then...

How can I express that with LINQ?

Upvotes: 0

Views: 54

Answers (2)

Ajay
Ajay

Reputation: 6590

Try this

Use Except:

var count = list2.Except(list1).Count();

or

var count = list2.Intersect(list1).Count();

or

var count = list2.Count(x => list1.Contains(x));

Upvotes: 0

MarcinJuraszek
MarcinJuraszek

Reputation: 125630

var count = list2.Count(x => list1.Contains(x));

or

var count = list2.Intersect(list1).Count();

Upvotes: 3

Related Questions