For Example, If I have the following datatable,
Location | First name | Pincode | Manager |
---|---|---|---|
Sydney | John | 123 | Brian |
New York | Larry | 456 | Sherry |
Chicago | Meg | 789 | Linda |
Dallas | Mark | 012 | Cooper |
Sydney | Jack | 123 | Brian |
Dallas | Chandler | 012 | Cooper |
Sydney | Richard | 123 | Brian |
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.
Location | First Name | Pincode | Manager |
---|---|---|---|
Sydney | John,Jack,Richard | 123 | Brian |
New York | Larry | 456 | Sherry |
Chicago | Meg | 789 | Linda |
Dallas | Mark,Chandler | 012 | Cooper |
I have stored this In a Datatable variable dt as follows:
DataTable dt = new DataTable();
using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.AppSettings["connectionString"].ToString()))
{
sqlConn.Open();
using (SqlCommand cmd = new SqlCommand(script, sqlConn))
{
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
adapter.SelectCommand.CommandTimeout = 3 * 60;
adapter.Fill(dt);
}
}
}