C# Object Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
To remove list items that exist in another list, you can use LINQ to create a new list that only contains items that do not exist in the other list. Here's an example:
List<int> numbers1 = new List<int>() { 1, 2, 3, 4, 5 }; List<int> numbers2 = new List<int>() { 3, 4, 5, 6, 7 }; List<int> result = numbers1.Except(numbers2).ToList();
In this example, we have two lists of integers, numbers1
and numbers2
. We use the Except
method to create a new list that contains only the items in numbers1
that do not exist in numbers2
. We then convert the result to a new list using the ToList
method.
To remove list items by a property that exists in another list, you can use LINQ to create a new list that only contains items that do not have a matching property value in the other list. Here's an example:
class MyClass { public int Id { get; set; } public string Name { get; set; } } List<MyClass> list1 = new List<MyClass>() { new MyClass() { Id = 1, Name = "John" }, new MyClass() { Id = 2, Name = "Mary" }, new MyClass() { Id = 3, Name = "Bob" }, new MyClass() { Id = 4, Name = "Alice" }, }; List<MyClass> list2 = new List<MyClass>() { new MyClass() { Id = 1, Name = "John" }, new MyClass() { Id = 3, Name = "Bob" }, }; List<MyClass> result = list1.Where(x => !list2.Any(y => y.Id == x.Id)).ToList();
In this example, we have two lists of MyClass
objects, list1
and list2
. We use LINQ to create a new list that contains only the items in list1
that do not have a matching Id
property value in list2
. We do this by using the Where
method to filter the items in list1
, and the Any
method to check if there are any items in list2
that have a matching Id
property value. We then convert the result to a new list using the ToList
method.
Removing common elements from two lists in C#:
List<T> uniqueList = list1.Except(list2).ToList();
Remove items from list if they exist in another list in C#:
list1.RemoveAll(item => list2.Contains(item));
C# LINQ remove items from one list based on another list:
List<T> uniqueList = list1.Where(item => !list2.Contains(item)).ToList();
Remove common objects between two lists in C#:
List<T> uniqueList = list1.Except(list2).ToList();
C# remove objects from list using Except:
List<T> uniqueList = list1.Except(list2).ToList();
Exclude elements from one list that are in another list in C#:
List<T> uniqueList = list1.Except(list2).ToList();
C# remove objects from list based on another list condition:
list1.RemoveAll(item => list2.Any(otherItem => /* condition to match items */));
Remove items from List<T> using Intersect in C#:
List<T> uniqueList = list1.Intersect(list2).ToList();
C# remove objects from list using Where and Any:
list1 = list1.Where(item => !list2.Any(otherItem => /* condition to match items */)).ToList();
Remove elements from List<T> if they exist in another list in C#:
list1.RemoveAll(item => list2.Contains(item));
C# remove objects from list using Except with custom equality comparison:
List<T> uniqueList = list1.Except(list2, new CustomEqualityComparer()).ToList();
Remove items from ObservableCollection based on another ObservableCollection in C#:
foreach (var item in collection2) { collection1.Remove(item); }
C# remove objects from list using LINQ and Contains:
list1 = list1.Where(item => !list2.Contains(item)).ToList();
Remove objects from HashSet if they exist in another HashSet in C#:
hashSet1.ExceptWith(hashSet2);
C# remove objects from list based on another list using RemoveAll:
list1.RemoveAll(item => list2.Contains(item));
Remove common elements from two arrays in C#:
T[] uniqueArray = array1.Except(array2).ToArray();
C# remove items from list based on another list property:
list1.RemoveAll(item => list2.Any(otherItem => otherItem.PropertyToCheck == item.PropertyToCheck));
Remove objects from ConcurrentDictionary if they exist in another ConcurrentDictionary in C#:
foreach (var key in dictionary2.Keys) { dictionary1.TryRemove(key, out _); }
C# remove objects from list using Intersect with custom equality comparison:
List<T> uniqueList = list1.Intersect(list2, new CustomEqualityComparer()).ToList();