Fastest Way to Find Common Items Across Multiple Lists in C#

When working with collections in C#, you may often encounter situations where you need to find common elements across multiple lists. This is particularly useful when you want to filter results based on specific criteria. In this blog post, we’ll address a common scenario: how to quickly identify a subset of Option objects that appear in every list within a list of lists.

The Problem Statement

Imagine you have a list of lists, specifically a structure like List<List<Option>> optionLists. You need to determine which Option objects are present in all N lists. To solve this, we need to be aware that the equality of the Option objects is determined by a specific string property, such as option1.Value == option2.Value. Ultimately, we want to return a List<Option> where each item appears only once.

The Solution: Finding Common Options

To tackle this problem, we can utilize LINQ (Language Integrated Query) in C#. LINQ provides a powerful way to query collections, making it ideal for our use case. Below is an effective code snippet that accomplishes this task:

var x = from list in optionLists
        from option in list
        where optionLists.All(l => l.Any(o => o.Value == option.Value))
        orderby option.Value
        select option;

Explanation of the Code

Let’s break down the LINQ query step by step:

  1. Outer From Clause: The first part from list in optionLists iterates through each list in our main list of lists (optionLists).

  2. Inner From Clause: The second part from option in list accesses each Option within the current list.

  3. Where Clause: This is the crucial part of the solution:

    • The condition where optionLists.All(l => l.Any(o => o.Value == option.Value)) checks if the current option is found in all lists within optionLists.
    • The Any method checks if there exists any Option o in the current list l where the Value matches the Value of the current option.
  4. Ordering the Results: The orderby option.Value clause sorts the resulting Option objects based on their Value.

  5. Select Clause: Finally, select option gathers all the eligible options into a collection.

Important Considerations

  • Distinct Values: The provided code does not include a distinct selection, which means it can return multiple Option objects with the same Value. If you need unique Options, consider appending .Distinct() to your query.

  • Performance: Depending on the size of your lists, the efficiency of this approach is significant. LINQ queries are optimized and can perform well with moderate-sized datasets.

Conclusion

Finding common items across multiple lists can be efficiently achieved in C# using LINQ, as illustrated in our example. By leveraging the power of LINQ, we can perform complex queries with minimal coding effort. This not only speeds up your coding process but also enhances the readability and maintainability of your code.

Now that you’re equipped with this knowledge, you can easily implement such solutions in your projects, making your handling of collections simpler and more efficient.