How to remove the duplicate from list of list?

Asma'a Ahmed Numan 0 Reputation points
2023-05-18T08:36:42.4833333+00:00

Hi everyone

I want to ask about this issue

how to remove the duplicate rows from this list

List<List<string>> Rows_list = new List<List<string>>();

I hope to get help and answer

Thanks.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,199 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,282 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,292 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,016 Reputation points Microsoft Vendor
    2023-05-19T02:29:34.2133333+00:00

    Hi @Asma'a Ahmed Numan

    how to remove the duplicate rows from this list List<List<string>> Rows_list = new List<List<string>>();

    You can use the Distinct() method to remove the duplicate data.

    For example: the first row and third row have the same row, and each row have the duplicate value.

    List<List<string>> Rows_list = new List<List<string>>();
     
    Rows_list.Add(new List<string> { "Row 1", "Row 2", "Row 3", "Row 2", "Row 3" });  // first row
    Rows_list.Add(new List<string> { "Row 21", "Row 22", "Row 23", "Row 22", "Row 23" });  //second row
    Rows_list.Add(new List<string> { "Row 1", "Row 2", "Row 3", "Row 2", "Row 3" });   //third row
    

    To remove the duplicate rows (the first row and third row), we can create a ListEqualityComparer class like this (refer this thread):

        public class ListEqualityComparer<T> : IEqualityComparer<List<T>>
        {
            private readonly IEqualityComparer<T> _itemEqualityComparer;
    
            public ListEqualityComparer() : this(null) { }
    
            public ListEqualityComparer(IEqualityComparer<T> itemEqualityComparer)
            {
                _itemEqualityComparer = itemEqualityComparer ?? EqualityComparer<T>.Default;
            }
    
            public static readonly ListEqualityComparer<T> Default = new ListEqualityComparer<T>();
    
            public bool Equals(List<T> x, List<T> y)
            {
                if (ReferenceEquals(x, y)) return true;
                if (ReferenceEquals(x, null) || ReferenceEquals(y, null)) return false;
                return x.Count == y.Count && !x.Except(y, _itemEqualityComparer).Any();
            }
    
            public int GetHashCode(List<T> list)
            {
                int hash = 17;
                foreach (var itemHash in list.Select(x => _itemEqualityComparer.GetHashCode(x))
                                             .OrderBy(h => h))
                {
                    hash += 31 * itemHash;
                }
                return hash;
            }
        }
    

    Then use the following code to remove the duplicate rows:

    var list1 = Rows_list.Distinct(new ListEqualityComparer<string>()).ToList();
    

    After that, if you want to remove duplicate values in each row, use the following code:

    var list2 = list1.Select(c => c.Distinct().ToList()).ToList();
    

    So finally, the query statement as below:

    var Rows_list2 = Rows_list.Distinct(new ListEqualityComparer<string>()).ToList().Select(c => c.Distinct().ToList()).ToList();
    

    The output like this:

    User's image


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion