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: 5Hooray! 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.



