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:
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