Click here to Skip to main content
15,918,243 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two datatables i want to concat them ,and i would sort the

What I have tried:

this is my code

FileInfo existingFile = new FileInfo(@excelreport);
         using (ExcelPackage package = new ExcelPackage(existingFile))
         {
             ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
             int colCount = worksheet.Dimension.End.Column;  //get Column Count
             int rowCount = worksheet.Dimension.End.Row;
             DataTable table = new DataTable();
             table.Columns.Add("Emp_fullname", typeof(string));
             table.Columns.Add("Emp_nik", typeof(string));
             table.Columns.Add("Timer_Finger", typeof(string));

                 for (int i=1; i<rowCount; i++)
                 {
                     table.Rows.Add(worksheet.Cells[i+1, 12].Value.ToString(), worksheet.Cells[i+1, 1].Value, worksheet.Cells[i+1, 4].Value.ToString());
                 }
                 string[] values1 = new string[table.Rows.Count];


                 var ordered = table.AsEnumerable().OrderByDescending(row => row["Timer_Finger"]);
                 foreach (DataRow row in ordered)
                 {
                     for (int i = 0; i < table.Rows.Count; i++)
                     {
                         values1[i] = table.Rows[i]["Emp_fullname"].ToString() + " " + table.Rows[i]["Emp_nik"].ToString() +  " " +table.Rows[i]["Timer_Finger"].ToString();
                     }
                 }
                 label11.Text = values1[values1.Length -1];

                 table.Clear();
         }
         DataTable table2 = new DataTable();
         DataSet dset2 = new DataSet();
         dset2 = FingerLog.GetDataTop5();
         table2 = dset2.Tables[0];
         var order = table2.AsEnumerable().OrderBy(row => row["Timer_finger"]);
         string[] arrray = new string[table2.Rows.Count];
         foreach (DataRow row in order)
         {
             arrray = table2.Rows.OfType<DataRow>().Select(k => k[0].ToString() + " " + k[1].ToString() + " " + k[2].ToString()).ToArray();
         }
         label23.Text = arrray[0];



How can i solve this?
Posted
Updated 13-Apr-20 23:02pm
Comments
Maciej Los 14-Apr-20 1:57am    
On what condition?
Can you share sample data?
Member 14760154 14-Apr-20 2:05am    
This is my sample data
001600063 2020-04-14 10:54:45.147
001800081 2020-04-14 10:54:36.540
001800081 2020-04-14 10:54:10.440
001600063 2020-04-13 23:40:10.223
001800081 2020-04-13 11:33:57.000
this is from database sql server

this is second data
001800082 4/14/2020 11:01:52 AM
001600063 4/14/2020 11:02:01 AM
this is from excel file

actually i want to merge both data and sort them ,
Maciej Los 14-Apr-20 4:27am    
Is there any logic to "concat" data? Do you mean, you want to union them?
Member 14760154 14-Apr-20 4:59am    
yes i do,

i wish i can union them,
Maciej Los 14-Apr-20 5:06am    
Please, use "Reply" widget (on the right side of nick/login) to be sure that system will inform member about your reply.
See my answer.

Member 14760154 wrote:
This is my sample data
001600063 2020-04-14 10:54:45.147
001800081 2020-04-14 10:54:36.540
001800081 2020-04-14 10:54:10.440
001600063 2020-04-13 23:40:10.223
001800081 2020-04-13 11:33:57.000

this is from database sql server

this is second data
001800082 4/14/2020 11:01:52 AM
001600063 4/14/2020 11:02:01 AM

this is from excel file

actually i want to merge both data and sort them


Well, all you need to do is to use Linq ;) See:
C#
//create new datatable to store data from table1 and table 2
DataTable finaldt = new DataTable();
finaldt.Columns.AddRange(new DataColumn[]
	{
		new DataColumn("SomeNumber", typeof(string)), //use proper field name and type!
		new DataColumn("SomeDate", typeof(DateTime)) //use proper field name and type!
	});

DataTable dt1 = ds1.Table[0]; //refer to proper dataset and its table
DataTable dt2 = ds2.Table[0];

finaldt = dt1.AsEnumerable()
		.Concat(dt2.AsEnumerable())
		.OrderBy(r=> r.Field<DateTime>("SomeDate"))  //use poper field name!
		.Select(r=> finaldt.LoadDataRow(r.ItemArray, false))
		.CopyToDataTable();


For further details, please see:
Enumerable.Concat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) Method (System.Linq) | Microsoft Docs[^]
Enumerable.OrderBy Method (System.Linq) | Microsoft Docs[^]
DataTable.LoadDataRow Method (System.Data) | Microsoft Docs[^]
DataTableExtensions.CopyToDataTable Method (System.Data) | Microsoft Docs[^]

I was working on this LinqPad[^] script:
C#
DataTable dt1 = new DataTable();
dt1.Columns.AddRange(new DataColumn[]
	{
		new DataColumn("SomeNumber", typeof(string)),
		new DataColumn("SomeDate", typeof(DateTime))
	});
dt1.Rows.Add(new object[]{"001600063", DateTime.ParseExact("2020-04-14 10:54:45.147", "yyyy-MM-dd HH:mm:ss.fff", System.Globalization.CultureInfo.InvariantCulture)});
dt1.Rows.Add(new object[]{"001800081", DateTime.ParseExact("2020-04-14 10:54:36.540", "yyyy-MM-dd HH:mm:ss.fff", System.Globalization.CultureInfo.InvariantCulture)});
dt1.Rows.Add(new object[]{"001800081", DateTime.ParseExact("2020-04-14 10:54:10.440", "yyyy-MM-dd HH:mm:ss.fff", System.Globalization.CultureInfo.InvariantCulture)});
dt1.Rows.Add(new object[]{"001600063", DateTime.ParseExact("2020-04-13 23:40:10.223", "yyyy-MM-dd HH:mm:ss.fff", System.Globalization.CultureInfo.InvariantCulture)});
dt1.Rows.Add(new object[]{"001800081", DateTime.ParseExact("2020-04-13 11:33:57.000", "yyyy-MM-dd HH:mm:ss.fff", System.Globalization.CultureInfo.InvariantCulture)});

DataTable dt2 = new DataTable();
dt2.Columns.AddRange(new DataColumn[]
	{
		new DataColumn("SomeNumber", typeof(string)),
		new DataColumn("SomeDate", typeof(DateTime))
	});
dt2.Rows.Add(new object[]{"001800082", DateTime.ParseExact("4/14/2020 11:01:52 AM", "M/dd/yyyy hh:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture)});
dt2.Rows.Add(new object[]{"001600063", DateTime.ParseExact("4/14/2020 11:02:01 AM", "M/dd/yyyy hh:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture)});

DataTable finaldt = new DataTable();
finaldt.Columns.AddRange(new DataColumn[]
	{
		new DataColumn("SomeNumber", typeof(string)),
		new DataColumn("SomeDate", typeof(DateTime))
	});

finaldt = dt1.AsEnumerable()
		.Concat(dt2.AsEnumerable())
		.OrderBy(r=> r.Field<DateTime>("SomeDate"))
		.Select(r=> finaldt.LoadDataRow(r.ItemArray, false))
		.CopyToDataTable();

finaldt.Dump();
 
Share this answer
 
Without knowing what happens that you didn't expect or doesn't happen that you did - and you forgot to tell us that bit - I can see one immediate problem:
C#
string[] arrray = new string[table2.Rows.Count];
foreach (DataRow row in order)
{
    arrray = table2.Rows.OfType<DataRow>().Select(k => k[0].ToString() + " " + k[1].ToString() + " " + k[2].ToString()).ToArray();
}
Each time you go round that loop, you throw away the previous iteration's results instead of adding the new data to it. You don't even use the row data inside the loop!

That may be the cause of your problem, but since we don't have any access to your data or any idea what you have noticed that is wrong, we can't really tell...
 
Share this answer
 
Comments
Member 14760154 14-Apr-20 2:45am    
Actually my problem is ,i want to merge data from database and data from an excel file,
and then i will sort them ,

i get data from database using store procedure ,
and data from excel i get using eppplus
OriginalGriff 14-Apr-20 2:54am    
And?
Where are you stuck?
What have you tried? (Other than the code above)
Whet help do you need?

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