Custom Extension Method to Compare List in C#

In this article, we will see how to create a custom extension method to compare 2 lists in C#.

Custom Extension Method

Create a static class ExtensionDemo. Inside that, have a static method "CompareList" of generic type and return type of bool. Make sure to have 2 List parameters and the first one should have "this" keyword. This parameter here refers to the list in which you are able to see this custom extension method.

I have used the system.reflection.propertyinfo namespace to compare each property in an object. To know more about comparing properties inside 2 objects using Properties, please check this article.

public static class ExtensionDemo

{
    public static bool CompareList<T>(this List<T> list1, List<T> list2)
    {
       
//if any of the list is null, return false

        if ((list1 == null && list2 != null) || (list2 == null && list1 != null))
            return false;

        //if both lists are null, return true, since its same
        else if (list1 == null && list2 == null)
            return true;
        //if count don't match between 2 lists, then return false
        if (list1.Count != list2.Count)
            return false;
        bool IsEqual = true;
        foreach (T item in list1)
        {
            T Object1 = item;
            T Object2 = list2.ElementAt(list1.IndexOf(item));
            Type type = typeof(T);
            //if any of the object inside list is null and other list has some value for the same object  then return false
                if ((Object1 == null && Object2 != null) || (Object2 == null && Object1 != null))
{
    IsEqual = false;
    break;
}
 
foreach (System.Reflection.PropertyInfo property in type.GetProperties())
{
    if (property.Name != "ExtensionData")
    {
        string Object1Value = string.Empty;
        string Object2Value = string.Empty;
        if (type.GetProperty(property.Name).GetValue(Object1, null) != null)

                            Object1Value = type.GetProperty(property.Name).GetValue(Object1, null).ToString();
                        if (type.GetProperty(property.Name).GetValue(Object2, null) != null)
                            Object2Value = type.GetProperty(property.Name).GetValue(Object2, null).ToString();
                        //if any of the property value inside an object in the list didnt match, return false
                    if (Object1Value.Trim() != Object2Value.Trim())
                    {
                        IsEqual = false;
                        break;
                    }
                }

            }
 
        }
        //if all the properties are same then return true
        return IsEqual;
    }

}

How to use the created custom extension method.

To test this, we have to create a new Web/Window/Console project. I am using a web application. I have created a student class as shown below in the same namespace.

public class Student
{
    public int? studentId { get; set; }
    public int age { get; set; }
}


Now in page_load event, create a list of objects for students.

protected void Page_Load(object sender, EventArgs e)
{
    List<Student> s1 = new List<Student>()
    {
        new Student(){studentId=100,age=21},
        new Student(){studentId=101,age=21},
        new Student(){studentId=102,age=21}
    };
    List<Student> s2 = new List<Student>()
    {
        new Student(){studentId=100,age=21},
        new Student(){studentId=101,age=21},
        null
    };
    List<Student> s3 = new List<Student>()
    {
        new Student(){studentId=100,age=21},
        new Student(){studentId=102,age=21},
        new Student(){studentId=102,age=21}

    };
    List<Student> s4 = new List<Student>()
    {
        new Student(){studentId=100,age=21},
        new Student(){studentId=101,age=21},
        new Student(){studentId=102,age=21}
    };
    List<Student> s5 = new List<Student>()
    {
        new Student(){studentId=100,age=21},
        new Student(){studentId=101,age=21},
        new Student(){studentId=102,age=22}
    };


Now we are going to compare 2 lists using the extension method. You will be able to see the created custom extension in the list of extension methods for list s1 as below.

ext.jpg

Now I compared a few combinations to verify that the method is working in the page_load event.

//compare 2 lists with different counts
            bool same = s1.com
            //bool same = s1.CompareList(s2);
            //compare 2 lists with same number of objects but one of the property inside an object is
different. Student id property
            bool same1 = s1.CompareList(s3);
            //compare 2 lists with same count and values
            bool same2 = s1.CompareList(s4);
            //compare 2 lists with same number of objects but one of the property inside an object is different.Age property
            bool same3 = s1.CompareList(s5);
            //compare 2 lists in which a list is null
            List<Student> s6= null;         
            bool same4 = s1.CompareList(s6);
            List<Student> s7= null;
            //compare 2 lists and both are null
            bool same5 = s6.CompareList(s7);
            s1 = new List<Student>()
            {
                new Student(){studentId=100,age=21},
                new Student(){studentId=101,age=21},
                null
            };
            //compare 2 lists in which one of the object inside a list is null
            bool same6 = s1.CompareList(s4);



While debugging, same2 and same5 will return true, all the remaining items will return false.

Please find the attached complete source code.
 

Up Next
    Ebook Download
    View all
    Learn
    View all