DEV Community

Kenichiro Nakamura
Kenichiro Nakamura

Posted on

24 3

C#: Compare two List items

Linq has useful method to compare two lists.

Intersect

By using Intersect, we can check which elements in source list are also contained in compare list.



var source = new List<string>() { "a", "b", "c" };
var compare = new List<string>() { "b", "c", "d" };
var result = source.Intersect(compare);


Enter fullscreen mode Exit fullscreen mode

The result is below.

Image description

Except

By using Except, we can check which elements in source list are not contained in compare list.



var source = new List<string>() { "a", "b", "c" };
var compare = new List<string>() { "b", "c", "d" };
var result = source.Except(compare);


Enter fullscreen mode Exit fullscreen mode

Image description

Complex type

We can also use complex type to compare list, by specifying which property to compare. If we use Person model for example, we cannot simply use Intersect nor Except. Following code result has no value.



var source = new List<Person>() { new Person("Ken", "Nakamura"), new Person("Nozomi", "Nakamura") };
var compare = new List<Person>() { new Person("Ken", "Nakamura"), new Person("Keiko", "Nakamura") };
var result = source.Intersect(compare);
Console.ReadLine();

public class Person
{
    public Person(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }
    public string FirstName { get; set; } = string.Empty;
    public string LastName { get; set; } = string.Empty;
}


Enter fullscreen mode Exit fullscreen mode

Use Comparer

Both method takes comparer as second argument. Let's compare by LastName.



public class PersonComparer : IEqualityComparer<Person>
{
    public bool Equals(Person x, Person y)
    {
        return x.LastName == y.LastName;
    }

    public int GetHashCode(Person x)
    {
        return x.LastName.GetHashCode();
    }
}


Enter fullscreen mode Exit fullscreen mode

Pass this class instance as second argument of Intersect.



var result = source.Intersect(compare, new PersonComparer());


Enter fullscreen mode Exit fullscreen mode

Image description

Use IntersectBy method

If we simply want to compare elements by key, then we can use IntersectBy instead of creating comparer. Following code generates same output as using comparer.



var source = new List<Person>() { new Person("Ken", "Nakamura"), new Person("Nozomi", "Nakamura") };
var compare = new List<Person>() { new Person("Ken", "Nakamura"), new Person("Keiko", "Nakamura") };
var result = source.IntersectBy(compare.Select(x => x.LastName), x => x.LastName);


Enter fullscreen mode Exit fullscreen mode

Use IEquatable

Other way to achieve the same is to have IEquatable for class itself.



public class Person: IEquatable<Person>
{
    public Person(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }

    public string FirstName { get; set; } = string.Empty;
    public string LastName { get; set; } = string.Empty;

    public bool Equals(Person? other) => this.LastName == other?.LastName;
    public override int GetHashCode() => (LastName).GetHashCode();
}


Enter fullscreen mode Exit fullscreen mode

Then we can simply call Intersect.



var result = source.Intersect(compare);

Enter fullscreen mode Exit fullscreen mode




Summary

There are multiple ways to compare two lists which depends on our implementation.

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (2)

Collapse
 
leorin profile image
kemal

Good article thanks.

var result = source.Intersect(compare, new PersonComparer());
Enter fullscreen mode Exit fullscreen mode

This code brings Ken Nakamura not because of Intersect "Ken"+"Nakamura" but just for "Nakamura". If your source list contains "Leo"+"Nakamura" as first element, then intersect would return Leo Nakamura object.

Collapse
 
kenakamu profile image
Kenichiro Nakamura

so true. Thanks!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay