title: Compare two DataRows excluding some columns
body: <p>I have two DataTables, e.g. OriginalEntity and Entity.
  Interface application modifies Entity dataTable's rows. While saving I want to check DataRows that is modified or different from OrigianlEntity.
  But, also I need to exclude few fields while comparing e.g. modified date and other audit fields.
  Currently I am looping through each rows of datatable, like this:</p>
  
  <pre><code>List&lt;string&gt; auditFields = new List&lt;string&gt;(){"createdon","modifiedon"};
  string IdentityKeyName = "id";
  object ent,orgEnt; 
  foreach(string columnName in columnList) // ColumnList is List of columns available in datatable
  {
      foreach(DataRow dr in Entity.Rows)
      {
          ent = dr[columnName];
          orgEnt = OriginalEntity.Select(IdentityKeyName + " = " + dr[IdentityKeyName].ToString())[0][columnName];
          if(!ent.Equals(orgEnt) &amp;&amp; !auditFields.Contains(columnName))
          {
              isModified = true;
              break;
          }
      }
  }
  </code></pre>
  
  <p>I just want an efficient way to achieve above. Please suggest.</p>
  
  <p>Thanks everyone for your suggestion, and this is my (as I don't have primary key defined)</p>
  
  <p>Solution:</p>
  
  <pre><code>public bool isModified(DataTable dt1, DataTable dt2, string IdentityKeyName)
  {
      bool isModified = false;
      List&lt;string&gt; auditFields = new List&lt;string&gt;() { "createdon", "modifiedon" };
      isModified = isModified || (dt1.Rows.Count != dt2.Rows.Count);
      if(!isModified)
      {
          //Approach takes 150 ms to compare two datatable of 10000 rows and 24 columns each
          DataTable copyOriginalEntity = dt1.Copy();
          DataTable copyEntity = dt2.Copy();
  
          //Exclude field you don't want in your comparison -- It was my main task
          foreach(string column in auditFields)
          {
              copyOriginalEntity.Columns.Remove(column);
              copyEntity.Columns.Remove(column);
          }
          for(int i=0;i&lt;copyOriginalEntity.Rows.Count;i++)
          {
              var origItems = copyOriginalEntity.Rows[i].ItemArray;
              var entityItem = copyEntity.Select(IdentityKeyName + " = " + copyOriginalEntity.Rows[i][dentityKeyName].ToString())[0].ItemArray;
              if(string.Concat(origItems) != string.Concat(entityItem)){ isModified = true; break; }
          }
      }
      return isModified;
  }
  </code></pre>