Hello ,
I tried your code and it works fine.
Try like this way ...
Console.WriteLine("--------First Table DataRows-----------");
DataTable dt1=new DataTable();
dt1.Columns.Add("EmailId");
dt1.Rows.Add("deepthi@gmail.com,divya@gmail.com,indra@gmail.com,lavanya@gmail.com");
dt1.Rows.Add("test@gmail.com,test1@gmail.com,test2@gmail.com");
dt1.AcceptChanges();
Now convert this comma separated rows into columns
int previousLength=dt1.Rows.Count;
for (int i = 0; i < previousLength; i++)
{
string[] vals = dt1.Rows[i][0].ToString().Split(',');
if (vals.Length > 1)
{
foreach (string s in vals)
{
DataRow newRow = dt1.NewRow();
newRow[0] = s;
dt1.Rows.Add(newRow);
}
}
}
for (int i = 0; i < previousLength; i++)
{
dt1.Rows.RemoveAt(0);
}
dt1.AcceptChanges();
Now adding second DataTable
Console.WriteLine("\n----------Second Table DataRows ----------");
DataTable dt2 = new DataTable();
dt2.Columns.Add("EmailId");
dt2.Rows.Add("deepthi@gmail.com");
dt2.Rows.Add("test1@gmail.com");
dt2.AcceptChanges();
Make a new datatable to store the match items from above said two tables
DataTable dt3=new DataTable();
dt3.Columns.Add("EmailId");
foreach (DataRow row1 in dt2.Rows)
{
foreach (DataRow row2 in dt1.Rows)
{
if (row1["EmailId"].ToString() == row2["EmailId"].ToString())
{
dt3.ImportRow(row2);
dt3.AcceptChanges();
}
}
}
and the out put is given below
---Out put of matching items----
deepthi@gmail.com
test1@gmail.com
Thanks