locked
Arranging same strings in Datatable RRS feed

  • Question

  • User185076547 posted

    My table,

    firstname 40 azar@gmail.com
    SecondName 30 vijay@gmail.com
    thirdName 45 azar1308@gmail.com
    fourthName 43 vijay@gmail.com

    Iwant to split this DataTable into Two DataTable like below,

    1st datatable

    firstname   40 azar@gmail.com

    thirdname  45 azar@gmail.com

    ---------------------------------

    2nd datatable

    secondName 30 vijay@gmail.com

    fourthName   43 vijay@gmail.com

    Using EmailID it should go....

    Friday, August 21, 2015 8:54 AM

Answers

  • User2103319870 posted

    Azarudeen

    Iwant to split this DataTable into Two DataTable base on Email,

    You can use Linq to split datatable based on Email id column

    	        DataTable table1 = new DataTable();
                    table1.Columns.Add("Name", typeof(string));
                    table1.Columns.Add("age", typeof(int));
                    table1.Columns.Add("Email", typeof(string));
                    // Add Three rows with those columns filled in the DataTable.
                    table1.Rows.Add("firstname", 40, "azar@gmail.com");
                    table1.Rows.Add("SecondName", 30, "vijay@gmail.com");
                    table1.Rows.Add("thirdName", 45, "azar@gmail.com");
                    table1.Rows.Add("fourthName", 43, "vijay@gmail.com");
    		//Resulte will contain two list with fetched based on Email
                    List<DataTable> result = table1.AsEnumerable()
                                    .GroupBy(row => row.Field<string>("Email"))
                                    .Select(g => g.CopyToDataTable())
                                    .ToList();

    Ensure that you have the below namespace in your page

    using System.Linq;

    To access the values from List you can use ForEach loop like below

                            //Get values from First list
    foreach(DataRow dr in result[0].Rows) { } //Get values from second list foreach(DataRow dr in result[1].Rows) { }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 21, 2015 2:23 PM

All replies

  • User2103319870 posted

    Azarudeen

    Iwant to split this DataTable into Two DataTable base on Email,

    You can use Linq to split datatable based on Email id column

    	        DataTable table1 = new DataTable();
                    table1.Columns.Add("Name", typeof(string));
                    table1.Columns.Add("age", typeof(int));
                    table1.Columns.Add("Email", typeof(string));
                    // Add Three rows with those columns filled in the DataTable.
                    table1.Rows.Add("firstname", 40, "azar@gmail.com");
                    table1.Rows.Add("SecondName", 30, "vijay@gmail.com");
                    table1.Rows.Add("thirdName", 45, "azar@gmail.com");
                    table1.Rows.Add("fourthName", 43, "vijay@gmail.com");
    		//Resulte will contain two list with fetched based on Email
                    List<DataTable> result = table1.AsEnumerable()
                                    .GroupBy(row => row.Field<string>("Email"))
                                    .Select(g => g.CopyToDataTable())
                                    .ToList();

    Ensure that you have the below namespace in your page

    using System.Linq;

    To access the values from List you can use ForEach loop like below

                            //Get values from First list
    foreach(DataRow dr in result[0].Rows) { } //Get values from second list foreach(DataRow dr in result[1].Rows) { }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 21, 2015 2:23 PM
  • User185076547 posted

    How can we do if teamleaderEmailID count is not known. I cannot use foreach because i dont know much will come

    Saturday, August 22, 2015 4:25 AM
  • User-271186128 posted

    Hi Azarudeen,

    How can we do if teamleaderEmailID count is not known. I cannot use foreach because i dont know much will come

    Could you please explain more details about your problem? And, if possible I suggest you post the column name in your data table.

    According to A2H's reply, it seems that the split method is just related to the Emails, what do you mean about the teamleaderEmailID count?

    Best Regards,
    Dillion

    Monday, August 24, 2015 1:36 AM