I have Table user data like
username id
aravind 1
mahesh 2
wilson 1
mike 3
john 3
suresh 2
i want out put like
id username
1 aravind,wilson
2 mahesh,suresh
3 mike,john
Please Help me
Thanks
Hemant SrivastavaPosted Nov 8, 2012, 6:31 PM
http://www.c-sharpcorner.com/UploadFile/0f68f2/querying-a-data-table-using-select-method-and-lambda-express/
Please let me know, if you need any clarification on the above code. Hope it should work for you.
If this solution helps you, Please mark it accepted.
Thanks
Hemant SrivastavaPosted Nov 8, 2012, 6:28 PM
class UserDataTableFinder
{
public void Execute()
{
DataTable dt = new DataTable();
dt.Columns.Add("USERNAME", typeof(string));
dt.Columns.Add("ID", typeof(string));
DataColumn[] keys = new DataColumn[1];
dt.Rows.Add("aravind", "1");
dt.Rows.Add("mahesh", "2");
dt.Rows.Add("wilson", "1");
dt.Rows.Add("mike", "3");
dt.Rows.Add("john", "3");
dt.Rows.Add("suresh", "2");
Console.WriteLine("\n-----------------------------------------------------------------------------");
List listOfIDs = new List();
for (int i = 0; i < dt.Rows.Count; i++ )
{
string currentID = dt.Rows[i][1].ToString();
// Checking whether ID is already processed or not, If not only print the names
if (!listOfIDs.Contains(currentID))
{
Console.Write("\n" + currentID + "\t");
// Finding in the entire Datatable for the current ID
foreach (DataRow o in dt.Select("ID = '" + currentID + "'"))
{
Console.Write("\t" + o["USERNAME"] + "\t");
}
}
// Maintaining IDs history by adding each ID into list
listOfIDs.Add(currentID);
}
Console.WriteLine("\n-----------------------------------------------------------------------------");
Console.Read();
}
}