Find Differences Between Two Lists with LINQ
Practical LINQ examples for comparing two lists, returning missing items, duplicates, changed records and list differences in .NET applications.
Define the alternatives
Start by identifying the two sequences and the comparison you need. In .NET, a list is often a List<T>, but LINQ works with arrays, database query results and most IEnumerable<T> collections. The core question is whether you need symmetric set differences, values present in only one list, or record-by-record changes between lists of objects.
For simple value collections such as List<string> or List<int>, use Except to find items that exist in one list but not the other. Intersect returns values present in both lists. For objects, you can compare by a key property using Select, or implement a custom equality comparer when the default object comparison is not appropriate.
- Only in first list: first.Except(second).
- Only in second list: second.Except(first).
- In both lists: first.Intersect(second).
- All asymmetric differences: first.Except(second).Concat(second.Except(first)).
Compare meaningful differences
Except and Intersect remove duplicate values and use the default equality rules for the element type. For strings, comparison is case-sensitive by default. To ignore case, pass StringComparer.OrdinalIgnoreCase to the method. This also changes how potentially duplicate strings are treated in the result.
When comparing objects, the default comparer checks whether instances are the same reference unless the type overrides equality members. For most record comparisons, project by a stable identifier first. For example, existing.Select(x => x.Id).Except(incoming.Select(x => x.Id)) returns identifiers present in the existing collection but missing from the incoming one.
If a property can be null, using Join with null keys can produce unexpected matches or misses depending on the comparer. Protect the key by filtering null values or using a placeholder that cannot collide with real data. The same issue applies when combining values from different sources such as a database list and uploaded records.
LINQ queries are deferred. The comparison runs when the result is enumerated, such as during ToList, Count or a foreach loop. If the source lists change before enumeration, the result reflects those changes rather than the collection state at the point where the query was defined.
Practical trade-offs
Except is concise for removing one set of values from another, but it should not be treated as a change detector. If the same identifier appears multiple times in one list, Except collapses it to a single occurrence. Use Except when duplicate position and frequency are irrelevant, such as checking whether a tag is missing from a set.
For ordered lists where position matters, use SequenceEqual to test whether two sequences contain the same elements in the same order and quantity. This is suitable for comparing two arrays of bytes or pre-sorted identifiers, but it reports only equality or inequality rather than the specific differing positions.
For records where you need to find items added, removed and retained, treat one list as the destination and the other as the source. Use Except on the identifiers for removed items, Except in the opposite direction for added items, and Intersect for retained identifiers. This is predictable when identifiers uniquely identify a record.
Large in-memory collections can make repeated enumeration expensive. Materializing projected keys into a HashSet can reduce repeated lookups, while still being expressed with LINQ-style operations. Published set-based APIs do not themselves reveal performance for a specific collection size; performance must be measured in the target environment.
Choose for the use case
Use Except for an asymmetric difference when you need values from one list that are absent from another, such as displaying which imported file names were not found in the expected folder list. Use symmetric combination only when membership in either list is enough.
Choose Intersect when the result should contain only values common to both lists, such as confirming which requested device labels are present in the available list. Choose projection with Select when objects must be compared by a key property instead of whole-object equality.
Use Join with a composite key when you also need the matching records after finding the shared keys. A left join pattern, expressed as from left in oneList join right in otherList on left.Key equals right.Key into matches from right in matches.DefaultIfEmpty(), can identify unmatched source records while preserving the original objects.
For a strict ordered comparison, use SequenceEqual with the same comparer that matches the data. For fast membership checks after the first comparison, create a HashSet from one projected key set and test each key in the other list. The best option depends on whether you need asymmetric output, matched records, duplicate preservation or strict order.