Use LINQ to get items in one List<>, that are not in another List<>

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Use LINQ to get items in one List<>, that are not in another List<>

Using LINQ to Get Items in One List that are Not in Another List

Are you looking for a way to use LINQ to find items in one List, but not in another List? You're in luck! LINQ provides a simple and elegant solution to this problem. In this blog post, we'll explore how to use LINQ to get all the people in peopleList2 that are not in peopleList1. 🕵️‍♀️🕵️‍♂️

The Code

Let's start with the code provided:

class Program
{
    static void Main(string[] args)
    {
        List<Person> peopleList1 = new List<Person>();
        peopleList1.Add(new Person() { ID = 1 });
        peopleList1.Add(new Person() { ID = 2 });
        peopleList1.Add(new Person() { ID = 3 });

        List<Person> peopleList2 = new List<Person>();
        peopleList2.Add(new Person() { ID = 1 });
        peopleList2.Add(new Person() { ID = 2 });
        peopleList2.Add(new Person() { ID = 3 });
        peopleList2.Add(new Person() { ID = 4 });
        peopleList2.Add(new Person() { ID = 5 });
    }
}

class Person
{
    public int ID { get; set; }
}

The Challenge

Given the two lists peopleList1 and peopleList2, we want to find all the people in peopleList2 that are not present in peopleList1. In this case, the expected result is two people with IDs 4 and 5. 🧐

The Solution

To achieve this, we can use the Except method provided by LINQ. The Except method returns the set difference between two sequences, which means it returns the elements in the first sequence that do not appear in the second sequence. All we need to do is write a LINQ query that utilizes the Except method.

Here's the LINQ query you can use:

var peopleNotInList1 = peopleList2.Except(peopleList1);

In this query, the Except method is called on peopleList2, passing peopleList1 as the argument. The result will be a new sequence peopleNotInList1 that contains the people from peopleList2 that are not in peopleList1. 🙌

Putting It All Together

To see the results, you can modify the code as follows:

class Program
{
    static void Main(string[] args)
    {
        List<Person> peopleList1 = new List<Person>();
        peopleList1.Add(new Person() { ID = 1 });
        peopleList1.Add(new Person() { ID = 2 });
        peopleList1.Add(new Person() { ID = 3 });

        List<Person> peopleList2 = new List<Person>();
        peopleList2.Add(new Person() { ID = 1 });
        peopleList2.Add(new Person() { ID = 2 });
        peopleList2.Add(new Person() { ID = 3 });
        peopleList2.Add(new Person() { ID = 4 });
        peopleList2.Add(new Person() { ID = 5 });

        var peopleNotInList1 = peopleList2.Except(peopleList1);

        foreach (var person in peopleNotInList1)
        {
            Console.WriteLine("Person ID: " + person.ID);
        }
    }
}

class Person
{
    public int ID { get; set; }
}

When you run the modified code, the output will be:

Person ID: 4
Person ID: 5

Hooray! You have successfully used LINQ to get all the people in peopleList2 that are not in peopleList1. 🎉

Conclusion

Using LINQ to find items in one List but not in another List is made simple with the Except method. By following the steps outlined in this blog post, you can easily solve this problem in your own code. Give it a try and let us know how it works for you. Happy coding! 💻💪

Don't forget to share this blog post with other developers who might find it useful. And if you have any other interesting problems or challenges you'd like us to cover, let us know in the comments below. Let's keep the conversation going! 👇📝

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my