LINQ - Find all items in one list that aren't in another list

86,454

Solution 1

Try using .Except extension method (docs):

var result = list1.Except(list2);

will give you all items in list1 that are not in list2.

IMPORTANT: Even though there's a link provided to MSDN docs for the method, I'll point this out here: Except only works out of the box for collections of primitive types, for POCOs/objects you need to implement IEquatable on that object.

Solution 2

Try this:

var List2 = OriginalList.Where(item => !List1.Any(item2 => item2.ID == item.ID));

Solution 3

The easiest way is to use the Except method.

var deletedItems = list1.Except(joinItems);

This will return the set of items in list1 that's not contained in joinItems

Share:
86,454
John Humphreys
Author by

John Humphreys

I'm a cloud DevOps director with a strong history of software & service development spanning military embedded systems, financial platforms, big data metrics systems, and general DevOps platform services. For the last 6+, years I have focused on building cross-company platform SaaS & PaaS services spanning query, orchestration, monitoring and compute both in the cloud and on-premises. I have led significant efforts leveraging orchestration technologies (Kubernetes, Airflow), big data technologies (Apache Spark, Presto, Apache Drill, Hive, Data Lakes), and streaming technologies (e.g. Kafka / Messaging). I have driven DevOps and CI/CD practices, improving automation and decreasing release cycle duration repeatedly (leveraging kubernetes, GitLab, terraform, ansible, etc). I've led DevOps teams managing large scale infrastructure and services in both the Azure and AWS clouds, but am much more accustomed to working in AWS these days. I also keep a blog here -> https://coding-stream-of-consciousness.com. It is mostly to help me recall things in the future; but I like that it occasionally saves others time as well. I hope you find it useful.

Updated on July 08, 2022

Comments

  • John Humphreys
    John Humphreys almost 2 years

    I'm stuck with a LINQ query (or any other efficient means of accomplishing the same thing). Can someone show me how I can select all the items in one list that are not present in another list?

    Basically, I have a list I formed by matching items between two other lists. I need to find all the items in the first list that matches weren't found for. Can someone fill in the stars in the second LINQ query below with the query that would achieve this goal? If I were using TSQL, I would do SELECT * NOT IN (), but I don't think LINQ allows that.

    //Create some sample lists.
    List<IdentifierLookupData> list1 = new List<IdentifierLookupData> { /*Init */ };
    List<IdentifierLookupData> list2 = new List<IdentifierLookupData> { /*Init */ };
    
    //Find all items in list1 and list2 that match and store them in joinItems.
    var joinItems = (from d1 in list1
        join d2 in list2 on d1 equals d2
        select d1).ToList<IdentiferLookupData>();
    
    //Find all items in list1 not in joinItems.
    var deletedItems = (from d1 in list1
         ***select all items not found in joinItems list.***