Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my application i have two datatables where i will be getting mailds in the first data table and i will be getting the mail id of thwe user who has logged in into the application in second datatable.Now i need to compare the email which i am getting in second datatable and add that email id in third datatable only if it matches with first datatable.The problem is in my first datatable i am getting a row with values as deepthi@gmail.com,divya@gmail.com,indra@gmail.com,lavanya@gmail.com and in my second datatable i will get only one mail id that is the mail id of the user who has logged in into the application let us suppose it is divya@gmail.com now i need to compare these two datatables and add the matching email id into third datatable how can i do this

<pre lang="c#">DataTable dtusercheck = adm.checkvalidusersornot(ddl1.SelectedValue);
     string value = username.Replace(" ", string.Empty);
     DataTable dtuserinprojectornot = adm.checkuserisinprojectornot(value);
     DataTable dt3 = dtuserinprojectornot.Clone();
     foreach (DataRow row1 in dtusercheck.Rows)
     {
         foreach (DataRow row2 in dtuserinprojectornot.Rows)
         {
             if (row1["id"].ToString() == row2["id"].ToString())
             {
                 dt3.ImportRow(row2);
             }
         }
     }
     if (dt3.Rows.Count > 0)
     {
         lblErrMsg.Visible = true;
     }
     else
     {
         lblErrMsg.Visible = true;
     }


What I have tried:

i tried above code but the problem with my code is i am getting the email id's seperated by commas in first datatable how can i seperate them after commas and how can i compare two datatables after seperating with commas
Posted
Updated 16-Jan-18 22:12pm
v2

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;//Initially keep the number of rows into a variable
		
		for (int i = 0; i < previousLength; i++)
        {
            //First, get your split values.
            string[] vals = dt1.Rows[i][0].ToString().Split(',');

            //only operate on rows that were split into multiples.
            if (vals.Length > 1)
            {
                //Add a  new row for each item parsed from value string
                foreach (string s in vals)
                {
                    DataRow newRow = dt1.NewRow();                   
                    newRow[0] = s;
                    dt1.Rows.Add(newRow);
                }
            }
        }

					
		//Remove old rows
        for (int i = 0; i < previousLength; i++)
        {
            dt1.Rows.RemoveAt(0);
        }
		
		//Save the changes
		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
 
Share this answer
 
Comments
Animesh Datta 17-Jan-18 4:36am    
After adding/deleting the rows in the DataTable you must save the changes
by dt1.AcceptChanges()
kav@94 17-Jan-18 4:50am    
I am trying to put in a method and stop the execution of next lines of my code if it goes into else condition of my below method by using return keyword but it is still executing the next lines of my code after going in else condition.How can i stop my execution of next lines of code after going into else condition

public void Users()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Id", typeof(string)));
dt.Rows.Add("test1@test.com,test2@test.com,test3@test.com");
dt.Rows.Add("test4@test.com,test5@test.com,test6@test.com");
dt.Rows.Add("test7@test.com,test8@test.com,test9@test.com");
DataTable dtusercheck = dt;

DataTable dt2 = new DataTable();
dt2.Columns.Add(new DataColumn("Id", typeof(string)));
dt2.Rows.Add("test3@test.com");
DataTable dtuserinprojectornot = dt2;
DataTable dt3 = dtuserinprojectornot.Clone();

foreach (DataRow row1 in dtusercheck.Rows)
{
foreach (DataRow row2 in dtuserinprojectornot.Rows)
{
foreach (string emailId in row1["id"].ToString().Split(','))
{
if (emailId == row2["id"].ToString())
{
dt3.ImportRow(row2);
}
}
}
}
if (dt3.Rows.Count > 0)
{
lblErrMsg.Visible = true;
}
else
{
lblErrMsg.Visible = true;
return;
}
}
There's at least few ways to achieve that. I'm a fan of Linq, so i'd suggest to use Linq queries...

DataTable dt1 = new DataTable();
dt1.Columns.Add(new DataColumn("registeredmails", typeof(string)));
dt1.Rows.Add(new object[]{"deepthi@gmail.com,divya@gmail.com,indra@gmail.com,lavanya@gmail.com"});
dt1.Rows.Add(new object[]{"lanadelray@gmail.com,digimanus@gmail.com,overflow@gmail.com,lennykravitz@gmail.com"});
dt1.Rows.Add(new object[]{"gretavanfleet@gmail.com,direstraits@gmail.com,johnydeep@gmail.com,sia@gmail.com"});

DataTable dt2 = new DataTable();
dt2.Columns.Add(new DataColumn("loggedmails", typeof(string)));
dt2.Rows.Add(new object[]{"divya@gmail.com"});
dt2.Rows.Add(new object[]{"sia@gmail.com"});

DataTable dtTemp = new DataTable();
dtTemp.Columns.Add(new DataColumn("mail", typeof(string)));


dtTemp = dt1.AsEnumerable()
	.SelectMany(x=>x.Field<string>("registeredmails").Split(new string[]{","}, StringSplitOptions.RemoveEmptyEntries)) //split mails by delimiter
	.Where(m=> dt2.AsEnumerable().Any(d=>d.Field<string>("loggedmails")==m)) //get equal data
	.Select(t=> dtTemp.LoadDataRow(new object[]{t}, false)) //convert IEnumerable<string> into set of DataRow
	.CopyToDataTable(); //copy data into final DataTable
	
//dtTemp contains equal data (mails):
//divya@gmail.com 
//sia@gmail.com 


For further details, please see:
LINQ: .NET Language Integrated Query[^]
 
Share this answer
 
Comments
kav@94 17-Jan-18 4:17am    
I am trying to put in a method and stop the execution of next lines of my code if it goes into else condition of my below method by using return keyword but it is still executing the next lines of my code after going in else condition.How can i stop my execution of next lines of code after going into else condition

public void Users()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Id", typeof(string)));
dt.Rows.Add("test1@test.com,test2@test.com,test3@test.com");
dt.Rows.Add("test4@test.com,test5@test.com,test6@test.com");
dt.Rows.Add("test7@test.com,test8@test.com,test9@test.com");
DataTable dtusercheck = dt;

DataTable dt2 = new DataTable();
dt2.Columns.Add(new DataColumn("Id", typeof(string)));
dt2.Rows.Add("test3@test.com");
DataTable dtuserinprojectornot = dt2;
DataTable dt3 = dtuserinprojectornot.Clone();

foreach (DataRow row1 in dtusercheck.Rows)
{
foreach (DataRow row2 in dtuserinprojectornot.Rows)
{
foreach (string emailId in row1["id"].ToString().Split(','))
{
if (emailId == row2["id"].ToString())
{
dt3.ImportRow(row2);
}
}
}
}
if (dt3.Rows.Count > 0)
{
lblErrMsg.Visible = true;
}
else
{
lblErrMsg.Visible = true;
return;
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900