49

I have two lists:

List<int> listA     
List<int> listB

How to check using LINQ if in the listA exists an element wchich deosn't exists in the listB ? I can use the foreach loop but I'm wondering if I can do this using LINQ

Ian Goldby
  • 5,609
  • 1
  • 45
  • 81
Tony
  • 12,405
  • 36
  • 126
  • 226

7 Answers7

102

listA.Except(listB) will give you all of the items in listA that are not in listB

cadrell0
  • 17,109
  • 5
  • 51
  • 69
  • 3
    Yes, it will do this. But as this is a set based operation it's worth remembering that the resulting list will also remove duplicates present in either ListA or ListB. – Holf Jan 27 '16 at 15:56
38
if (listA.Except(listB).Any())
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
17
listA.Any(_ => listB.Contains(_))

:)

the_joric
  • 11,986
  • 6
  • 36
  • 57
  • 2
    This is best anwser, because solutions provided by @cadrell0 and SLaks will only check if A contains all elements of B but not if B has any more elements that don't match A. This solution is valid for example below. A: [0,1,2,3]; B: [0,1,2,3,4] `listA.Except(listB).Any() == false` `listA.Any(_ => listB.Contains(_)) == true` – Tomasz Juszczak Jan 02 '17 at 13:23
  • 2
    @TomaszJuszczak True, but the question specifically asked only if A contains anything not in B - i.e. it **should** evaluate to false if B contains something not in A provided A doesn't contain something not in B. So this answer is incorrect. (Yes, I misunderstood the question when I first read.) – Ian Goldby Mar 17 '17 at 10:03
  • As you're actually using the value from Any, you shouldn't use discard ( _ ) because it means (to dev and compiler) that you don't care about value – cdie Jan 22 '21 at 10:03
9

You can do it in a single line

var res = listA.Where(n => !listB.Contains(n));

This is not the fastest way to do it: in case listB is relatively long, this should be faster:

var setB = new HashSet(listB);
var res = listA.Where(n => !setB.Contains(n));
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
5

Get the difference of two lists using Any(). The Linq Any() function returns a boolean if a condition is met but you can use it to return the difference of two lists:

var difference = ListA.Where(a => !ListB.Any(b => b.ListItem == a.ListItem)).ToList();
Jnr
  • 1,504
  • 2
  • 21
  • 36
3

List has Contains method that return bool. We can use that method in query.

List<int> listA = new List<int>();
List<int> listB = new List<int>();
listA.AddRange(new int[] { 1,2,3,4,5 });
listB.AddRange(new int[] { 3,5,6,7,8 });

var v = from x in listA
        where !listB.Contains(x)
        select x;

foreach (int i in v)
    Console.WriteLine(i);
Onur
  • 599
  • 4
  • 12
2

This piece of code compares two lists both containing a field for a CultureCode like 'en-GB'. This will leave non existing translations in the list. (we needed a dropdown list for not-translated languages for articles)

var compared = supportedLanguages.Where(sl => !existingTranslations.Any(fmt => fmt.CultureCode == sl.Culture)).ToList();

Joost00719
  • 716
  • 1
  • 5
  • 20