2021年3月30日星期二

Best way to See if String in Hashset contains elements from another hashset? C#

I have a hashset of strings that contain lognames called pidLogsLines. From this hashset I want to filter out logs that only contain strings from another hashset called pidList and put these logs inside a new hashset called filesWithPid. I am doing this with two loops:

var filesWithPid = new HashSet<string>(StringComparer.OrdinalIgnoreCase);  foreach(var logname in pidLogsLines)  {      foreach(var pid in pidList)      {          if (logname.Contains(pid))          {              filesWithPid.Add(logname);          }      }    }  

Is this the optimal way to do it? I am new to C# so I am not aware of any elaborate faster ways to do it

https://stackoverflow.com/questions/66880310/best-way-to-see-if-string-in-hashset-contains-elements-from-another-hashset-c-s March 31, 2021 at 09:05AM

没有评论:

发表评论