Thursday, 15 May 2014

c# - add elements to int hashset from int array -



c# - add elements to int hashset from int array -

it possible iterate on array , add together elements hashset 1 one. there way add together integers int array int hashset without iterating on array elements??

int[] pagesid;//int array var deletepages = new hashset<int>();//hashset pagesid= array.convertall(text.split(','), s=>int.parse(s)); //values pagesid should added hashset.

update:

int[] pagesid;//int array var deletepages = new hashset<int>();//hashset foreach (xmlnode rule in pgmgmtrules) { ruleresult=doc.parsetext(rule.innertext, false);//parse rule if (ruleresult != "") { //if parsed rule result has value if (rule.attributes["action"].value == "delete") { var text=rule.attributes["pageids"].value; pagesid= array.convertall(text.split(','), s=>int.parse(s)); //add elements pagesid array hashset } } }

you can utilize overloaded constructor hashset constructor (ienumerable) takes ienumerable<t> parameter instead of using default constructor hashset<t>().

hashset<int> evennumbers = new hashset<int>(text.split(',').select(int.parse));

hashset constructor (ienumerable)

initializes new instance of hashset class uses default equality comparer set type, contains elements copied specified collection, , has sufficient capacity accommodate number of elements copied.

edit 1 if want filter number array can utilize where

var intarr = text.split(',').select(int.parse); hashset<int> evennumbers = new hashset<int>(intarr.where(i=>i%2==0));

edit 2 based on comments. can utilize list<int> instead of int array. keeping adding int pagesids in list in loop , when loop finished add together list in hashset through constructor hashset constructor (ienumerable) .

list<int> pagesid = new list<int>();//int array hashset<int> deletepages = null; foreach (xmlnode rule in pgmgmtrules) { ruleresult=doc.parsetext(rule.innertext, false);//parse rule if (ruleresult != "") { //if parsed rule result has value if (rule.attributes["action"].value == "delete") { var text=rule.attributes["pageids"].value; pagesid.addrange(text.split(',').select(int.parse)); } } } //add elements pagesid array hashset deletepages = new hashset<int>(pagesid);

c# asp.net arrays hashset

No comments:

Post a Comment