linq - Check whether a string is in a list at any order in C# -


if have list of strings following code:

        list<string> xall = new list<string>();         xall.add("#10#20");         xall.add("#20#30#40");          string s = "#30#20";//<- same #20#30 same "#20#30#40" means s exist in list         //check un-ordered string s=  #30#20         // if contained @ order #30#20 or #20#30  ..... return true :it exist          if (xall.contains(s))         {             console.writeline("your string exist");         } 

i prefer use linq check s in regard exist, no matter how order in list, contains both (#30) , (#20) [at least] in list xall.

i using

var c = item2.intersect(item1);                 if (c.count() == item1.length)                 {                     return true;                 } 

this works me:

    func<string, string[]> split =         x => x.split(new [] { '#' }, stringsplitoptions.removeemptyentries);      if (xall.any(x => split(x).intersect(split(s)).count() == split(s).count()))     {         console.writeline("your string exist");     } 

now, depending on you want handle duplicates, might better solution:

    func<string, hashset<string>> split =         x => new hashset<string>(x.split(                     new [] { '#' },                     stringsplitoptions.removeemptyentries));      if (xall.any(x => split(s).issubsetof(split(x))))     {         console.writeline("your string exist");     } 

this second approach uses pure set theory strips duplicates.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -