Search code examples
c#arraysstringlistlinq

One Way To Match Array of String and List of String


I am trying to match list of string and array string using foreach loop but in my case, it seems I require to iterate a lot to match corresponding values. I've the following:

string[] splitValues = item.ValuesWithComma.Split(',');

So it has indexes and in the same way, the list of string will have the equal indexes. So is it possible to match those directly with indexes as follows to avoid loops:

splitValues == item.lstOfValues;

Right now, I am iterating every time to match and it seems really inappropriate.

foreach (var item in lstOfValues)
{
   foreach (var item2 in splitValues )
   {
      //Condition goes here 
   }
}

N.B: Both the types may have equal no. of indexes and if unmatched, it'll be skipped. Match means here, the types may have equal no. of values.


Solution

  • You can use bool equal = ls1.SequenceEqual(ls2);, It will match the lists sequentially as well.