Adding list item from list<> after comparing using c# -


i have list control, in adding items. have generic list of list comparing, want compare items of list control, , if found match don't wants include in list list.

foreach( s s1 in s_list) {     if (ex.count > 0)  // if list not empmty     {         foreach(ex li in ex)  //          {             if (s1.name.equals(li.tostring()))             {                 flag=true;                 ex.add(s1.name);             }         }     }     else     {         ex.add(s1.name);     } } 

problem is:

its causing duplication in ex list, how done?

i'm not sure understood trying achieve, code looks suspicious me:

foreach(ex li in ex)  //  {     if (s1.name.equals(li.tostring()))     {         flag=true;         ex.add(s1.name);     } } 

so, add s1.name list every time when find same element in list? way ex populated elements equal first added element.

possibly job:

foreach( s s1 in s_list) {     boolean foundinex = false;     foreach(ex li in ex)  //      {         if (s1.name.equals(li.tostring()))         {             foundinex = true;             break;         }     }     if(!foundinex)      {         ex.add(s1.name); //only executed when there no such element in ex     } } 

shorter way using linq count:

foreach( s s1 in s_list) {     if(ex.count(li => s1.name.equals(li.tostring()))>0)      {         ex.add(s1.name); //only executed when there no such element in ex     } } 

also can use linq distinct if want unique elements list:

var uniquenames = s_list.select(x => x.name).distinct(); 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -