Remove duplicate lists from a parent list using hashset in c# -
i have parent list
contains several child lists
. , 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 child lists
contain different column
objects this:
list1 = {c1, c1, c2} list2 = {c1, c2, c1} list3 = {c2, c3} list4 = {c1,c1, c2}
and parent list
contains these lists
:
listofallcolumns = {list1, list2, list3, list4}
now want method removes duplicate lists listofallcolumns list. example, list above , remove list4.
list1: c1,c1,c2 list2: c1,c2,c1 list3: c2,c3 list4: c1,c1,c2 (it equal list1 duplicate)
i know can done using linq
, distinct
method want using hashset
.
- by way order important, example {c1, c2, c1} different {c2,c1,c1}.
update 1:
here tricky part. order important inside child lists not important in parent list. list1:{c1, c2} , list2: {c2,c1} different not important how added parent list
, listofallcolumns:{list1, list2} , listofallcolumns:{list2,lis1} same thing.
here code 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)) { // logic hashset should recognize duplicates in part. uniquelistofallcolumns.add(c); } }
a similar question here: c#: remove duplicate values dictionary?
you should override hashset
methods contains
, equals
(where can implement ordered checking).
then insert implementation of hashset
parentlist. check if hashset
contains list<column>
, insert if not there.
but linq
seems best solution.
Comments
Post a Comment