locked
Remove time zone RRS feed

  • Question

  • User1369593875 posted

    Hello,

    I hope you can help me.

    I have a datatable with different time zones, and I would like to join the rows where end date matches with start date of the next row.

    Start date       End date

    07:00       -        13:00

    13:00       -        15:00

    16:00       -        19:00

    The result should be:

    07:00       -        15:00

    16:00       -        19:00

    Thank you

    Wednesday, December 13, 2017 1:03 PM

All replies

  • User-335504541 posted

    <g class="gr_ gr_20 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="20" data-gr-id="20">Hi</g> basquepower,

    You could use the left join to connect the rows.

    Please try to use the following code:

                DataTable dt1 = new DataTable();
                dt1.Columns.Add("Start date");
                dt1.Columns.Add("End date");
                dt1.Rows.Add("07:00", "13:00");
                dt1.Rows.Add("13:00", "15:00");
                dt1.Rows.Add("16:00", "19:00");
               
    
                var data = (from table1 in dt1.AsEnumerable()
                           join table2 in dt1.AsEnumerable() on (string)table1["End date"] equals (string)table2["Start date"] into table3
                           from table in table3.DefaultIfEmpty()
                           select new { Startdate = table1["Start date"], Enddate = table == null ? table1["End date"] : table["End date"] }).ToList();
                var data2 = (from table1 in dt1.AsEnumerable()
                            join table2 in dt1.AsEnumerable() on (string)table1["End date"] equals (string)table2["Start date"] into table3
                            from table in table3
                            select new { Startdate = table["Start date"], Enddate = table["End date"] }).ToList();
    
    
                var result = (from d in data
                                    where !(data2.Contains(d))
                                    select d).ToList();
    
                DataTable dt2 = new DataTable();
                dt2.Columns.Add("Start date");
                dt2.Columns.Add("End date");
    
                foreach (var d in result)
                {                
                    dt2.Rows.Add(d.Startdate, d.Enddate);
                }

    And the result is:

    Best Regards,

    Billy

    Thursday, December 14, 2017 4:32 AM