I have a parent List
which contains several child Lists
. And these inner Lists
contain Column
objects.
List<List<Column>> listOfAllColumns;
public class Column
{
public string SectionName;
public string StirrupType;
public int StirrupSize;
public double StirrupSpacing;
}
Let's say my child Lists
contain different Column
objects like this:
List1 = {c1, c1, c2}
List2 = {c1, c2, c1}
List3 = {c2, c3}
List4 = {c1,c1, c2}
And the parent List
contains these Lists
:
listOfAllColumns = {List1, List2, List3, List4}
Now I want a method that removes duplicate lists from the listOfAllColumns list. For example, it will look into the list above and remove list4.
List1: c1,c1,c2
List2: c1,c2,c1
List3: c2,c3
List4: c1,c1,c2 (it is equal to List1 so it is a duplicate)
I know this can be done using Linq
and Distinct
method but I want to do it using Hashset
.
- By the way the order is important, so for example {c1, c2, c1} is different than {c2,c1,c1}.
Update 1:
Here is the tricky part. The order is important inside the child lists but it is not important in the parent list. So List1:{c1, c2} and List2: {c2,c1} are different but it is not important how they are added to the Parent List
, so listOfAllColumns:{List1, List2} and listOfAllColumns:{List2,Lis1} are the same thing.
Here is the code I have tried:
Column c1 = new Column() { SectionName = "C50", StirrupType = "Tie" };
Column c2 = new Column() { SectionName = "C50", StirrupType = "Spiral" };
Column c3 = new Column() { SectionName = "C40", StirrupType = "Tie" };
List<Column> list1 = new List<Column>() { c1, c1, c2 };
List<Column> list2 = new List<Column>() { c1, c2, c1 };
List<Column> list3 = new List<Column>() { c2, c3 };
List<Column> list4 = new List<Column>() { c1, c1, c2 };
List<List<Column>> listOfAllColumns = new List<List<Column>>() { list1, list2, list3, list4 };
HashSet<List<Column>> hs = new HashSet<List<Column>>();
List<List<Column>> uniquelistOfAllColumns = new List<List<Column>>();
foreach (var c in listOfAllColumns)
{
if (hs.Add(c))
{
// my logic is that hashset should recognize the duplicates in this part.
uniquelistOfAllColumns.Add(c);
}
}
A similar question here: C#: Remove duplicate values from dictionary?