title: Comparing two list of rows based on Data in a selected cell using Smartsheet API
body: <p>I want to compare two list of row data. Right now I want to see if the two list contain the same title in their respective cell values. </p>
  
  <p>What methods with using Smartsheet API C# could I use to sort through the list and compare each select element in each row?</p>
  
  <p>I already have a column name table to search for the column name and reference the actual column id. But I can not seem to fathom how? </p>
  
  <p>Any input would be helpful and I'm sorry if I sound plain dumb but I usually do not ask for help.</p>
  
  <p>I have two sheets in Smartsheet. One sheet contains all the data that is given and as it goes through a process of acceptance or rejection. If completely accepted it is given a status of "Moved to Project". When the code runs it will place all rows with that status to a List that will then be used to move and compare against other list. </p>
  
  <p>The Moved to Project List will be compared to our Project Management Active List. </p>
  
  <p>I am stuck at trying to compare cell values through the API and maybe I'm just looking at it wrong. I've tried Enum Except to compare list but it is not working and I'm thinking I will need to create a nested loop to sort through and compare each element.</p>
  
  <pre><code>        foreach (Row row in rowsToCompare)
          {
              Cell PMOPName = getPMOCellByColumnName(row, "Project Name");
              foreach (Row innerrow in rowsToMove)
              {
  
                  Cell MainTitle = getCellByColumnName(innerrow, "Title");
  
                  if (PMOPName.DisplayValue == MainTitle.DisplayValue)
                  {
                      Console.WriteLine("Yes");
                  }
                  else
                      Console.WriteLine("No");
              }
          }
  
  
  
      static Cell getCellByColumnName(Row row, string columnName)
      {
          return row.Cells.FirstOrDefault(cell =&gt; cell.ColumnId == 
          columnMap[columnName]);
      }
  
      static Cell getPMOCellByColumnName(Row row, string columnName)
      {
          return row.Cells.FirstOrDefault(cell =&gt; cell.ColumnId == 
          columnMapPMO[columnName]);
      }
  }
  </code></pre>
  
  <p>Whenever there is a match of title and project name it should output yes and if not a no. </p>
  
  <p>But instead I get a Unhandled Exception: System.ArgumentNullException: Value cannot be null.</p>
  
  <p>I've pinpointed it to the nested loop. I'm sure I just did something stupid.</p>
  
  <p>EDIT:
  So this is the definition of the map and how it get it's data.</p>
  
  <pre><code>static Dictionary&lt;string, long&gt; columnMap = new Dictionary&lt;string, long&gt;(); 
  
  static Dictionary&lt;string, long&gt; columnMapPMO = new Dictionary&lt;string, 
  long();
  
  // Build column map for later reference
     foreach (Column column in sheet.Columns)
        columnMap.Add(column.Title, (long)column.Id);
  
     foreach (Column column in pmosheet.Columns)
        columnMapPMO.Add(column.Title, (long)column.Id);
  </code></pre>
  
  <p>EDIT 2: Confirming with Tim the code works but in my instance it is still coming up with an error so I will place the code that I currently have as a whole to see if possible the other functions could be causing issues.</p>
  
  <pre><code>static void Main(string[] args)    
  {
  
   SmartsheetClient ss = new SmartsheetBuilder()
               // TODO: Set your API access in environment variable 
   SMARTSHEET_ACCESS_TOKEN or else here
               .SetAccessToken(token.AccessToken)
               .Build();
  
          var sheet = ss.SheetResources.GetSheet(
              sheetId,                    // long sheetId
              null,                       // IEnumerable&lt;SheetLevelInclusion&gt; 
              includes
              null,                       // IEnumerable&lt;SheetLevelExclusion&gt; 
              excludes
              null,                       // IEnumerable&lt;long&gt; rowIds
              null,                       // IEnumerable&lt;int&gt; rowNumbers
              null,                       // IEnumerable&lt;long&gt; columnIds
              null,                       // Nullable&lt;long&gt; pageSize
              null                        // Nullable&lt;long&gt; page
          );
  
          var pmosheet = ss.SheetResources.GetSheet(
              copyId,
              null,
              null,
              null,
              null,
              null,
              null,
              null
          );
  
          // Build column map for later reference
          foreach (Column column in sheet.Columns)
              columnMap.Add(column.Title, (long)column.Id);
  
          foreach (Column column in pmosheet.Columns)
              columnMapPMO.Add(column.Title, (long)column.Id);
  
          // Accumulate rows needing update and archive here
          List&lt;Row&gt; rowsToMove = new List&lt;Row&gt;();
          List&lt;Row&gt; rowsToArchive = new List&lt;Row&gt;();
          List&lt;Row&gt; rowsToCompare = new List&lt;Row&gt;();
  
          //Loops through the Ideation Sheet and execute function to evaluate 
          //each row and add those row to the move list.
  
          foreach (Row row in sheet.Rows)
          {
              Row rowToMove = evaluateRowAndBuildUpdates(row);
              if (rowToMove != null)
              { 
                  rowsToMove.Add(rowToMove);
              }
          }
          Console.WriteLine("\n");
  
          foreach (Row row in pmosheet.Rows)
          {
              Row rowtoCompare = compareRowandCopy(row);
              if (rowtoCompare != null)
                  rowsToCompare.Add(rowtoCompare);
          }
          Console.WriteLine("\n");
  
          foreach (Row innerrow in rowsToMove)
          {
              Cell MainTitle = getCellByColumnName(innerrow, "Title");
              foreach (Row row in rowsToCompare)
              {
  
                  Cell PMOPName = getPMOCellByColumnName(row, "Project Name");
  
                  if (PMOPName.DisplayValue == MainTitle.DisplayValue)
                  {
                      Console.WriteLine("Yes");
                      break;
                  }
                  else
                      Console.WriteLine("No");
              }
          }
  
            System.Environment.Exit(1); //End of Program
  }
  
   static Row evaluateRowAndBuildUpdates(Row sourceRow)
      {
          Row rowToUpdate = null;
  
          // Find cell we want to examine
          Cell statusCell = getCellByColumnName(sourceRow, "Status");
          if (statusCell.DisplayValue == "Moved to Project")
          {
              Cell remainingCell = getCellByColumnName(sourceRow, "Status");
              Cell titleCell = getCellByColumnName(sourceRow, "Title");
              if (remainingCell.DisplayValue == "Moved to Project")                  
              {
                  rowToUpdate = new Row
                  {
                      Id = sourceRow.Id,
  
                  };
                  Console.WriteLine("Ideation");
              }
  
              Console.WriteLine(titleCell.DisplayValue + " ID: " + 
              sourceRow.Id.ToString());
          }
          return rowToUpdate;
      }
  
      static Row compareRowandCopy(Row sourceRow)
      {
          Row rowToCopy = null;
  
          Cell pmoStatusCell = getPMOCellByColumnName(sourceRow, "Project 
          Name");
          if (pmoStatusCell.DisplayValue != null)
          {
              rowToCopy = new Row
              {
                  Id = sourceRow.Id,
  
              };
          }
          Console.WriteLine("PMO");
          Console.WriteLine(pmoStatusCell.DisplayValue + " ID: " + 
          sourceRow.Id.ToString());
          return rowToCopy;
      }
  
          static Cell getCellByColumnName(Row row, string columnName)
      {
          return row.Cells.FirstOrDefault(cell =&gt; cell.ColumnId == 
          columnMap[columnName]);
      }
  
      static Cell getPMOCellByColumnName(Row row, string columnName)
      {
          return row.Cells.FirstOrDefault(cell =&gt; cell.ColumnId == 
          columnMapPMO[columnName]);
      }
  </code></pre>