title: C# Datatable. Compare data from a column and if it matches, append the data from another column of the corresponding row
body: <p>For Example,
  If I have the following datatable,</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Location</th>
  <th>First name</th>
  <th>Pincode</th>
  <th>Manager</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>Sydney</td>
  <td>John</td>
  <td>123</td>
  <td>Brian</td>
  </tr>
  <tr>
  <td>New York</td>
  <td>Larry</td>
  <td>456</td>
  <td>Sherry</td>
  </tr>
  <tr>
  <td>Chicago</td>
  <td>Meg</td>
  <td>789</td>
  <td>Linda</td>
  </tr>
  <tr>
  <td>Dallas</td>
  <td>Mark</td>
  <td>012</td>
  <td>Cooper</td>
  </tr>
  <tr>
  <td>Sydney</td>
  <td>Jack</td>
  <td>123</td>
  <td>Brian</td>
  </tr>
  <tr>
  <td>Dallas</td>
  <td>Chandler</td>
  <td>012</td>
  <td>Cooper</td>
  </tr>
  <tr>
  <td>Sydney</td>
  <td>Richard</td>
  <td>123</td>
  <td>Brian</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Here, the 1st column to traverse would be Location. Wherever the Location matches, traverse all the corresponding First Names and keep it in a single row comma separated.</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Location</th>
  <th>First Name</th>
  <th>Pincode</th>
  <th>Manager</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>Sydney</td>
  <td>John,Jack,Richard</td>
  <td>123</td>
  <td>Brian</td>
  </tr>
  <tr>
  <td>New York</td>
  <td>Larry</td>
  <td>456</td>
  <td>Sherry</td>
  </tr>
  <tr>
  <td>Chicago</td>
  <td>Meg</td>
  <td>789</td>
  <td>Linda</td>
  </tr>
  <tr>
  <td>Dallas</td>
  <td>Mark,Chandler</td>
  <td>012</td>
  <td>Cooper</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I have stored this In a Datatable variable dt as follows:</p>
  <pre><code>DataTable dt = new DataTable();
  using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.AppSettings[&quot;connectionString&quot;].ToString()))
              {
                  sqlConn.Open();
                  using (SqlCommand cmd = new SqlCommand(script, sqlConn))
                  {
                      using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                      {
                          adapter.SelectCommand.CommandTimeout = 3 * 60;
                          adapter.Fill(dt);
                      }
                  }
              }
  </code></pre>