Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am attempting to compare an array to a list. The list contains 4 items that should be repeated in the array and in the same order. Previously all I had to do is make sure that each of the array values matched one of the list values, but now they must be validated to be the same and in the same order. This is what I had previously but am not sure how to change it. Can anyone help me with this one?

Range activity = this.excelWorkSheet.Columns.Range["D9:" + lastcell];
foreach (var value2 in activity.Value2)
{
  var listofoptions = new List<string>
  {
    "a",
    "b",
    "c",
    "d"
  };
  string metric = string.Empty;
  for (int i = 0; i < listofoptions.Count; i++)
  {
    if (value2.ToString() == listofoptions[i])
    {
      metric = listofoptions[i];
      Assert.IsTrue(value2.ToString().Contains(metric));
      break;
    }
  }
  if (metric == string.Empty)
  { 
    Assert.Fail("Metric Type Invalid");
  }
share|improve this question
add comment

closed as off-topic by Mat's Mug, megawac, Jamal Feb 8 at 4:17

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking for code to be written to solve a specific problem are off-topic here as there is no code to review." – Mat's Mug, megawac, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

I like to use

list.Zip(array, (listItem, arrayItem) => listItem == arrayItem).All(valuesEqual => valuesEqual)

This walks both collections, compares the elements, and ensures all comparisons evaluated to true.

Note that you will also have to compare the lengths, because this method stops as soon as one of the lists ends, and returns true.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.