Click here to Skip to main content
15,885,194 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a DataTable

id	StartTime EndTime  duration  ratio
1	   7	    11	    4	      1.99
2	  11	    15	    4	      1.99
3	  15	    19	    4	      1.99
4	  19	    23	    4	      1.99


Need output as below

StartTime EndTime  duration  ratio
   7	    15       8	      3.98
  15	    23	     8	      3.98


What I have tried is

C#
(from p in DataTable.AsEnumerable()
 join o in DataTable.AsEnumerable() 

                   on p["EndTime"].ToString() equals o["StartTime"].ToString()

 orderby (DateTime.ParseExact(p["StartTime"].ToString(), "HHmm", provider)).Hour

 where (int.Parse(p["Duration"].ToString()) + int.Parse(o["Duration"].ToString())) == 8

 select new
    {
       ID = p["ID"].ToString(),

       StartTime = Math.Min(DateTime.ParseExact(p["StartTime"].ToString(), "HHmm", provider).Hour, DateTime.ParseExact(o["StartTime"].ToString(), "HHmm", provider).Hour),

       EndTime =   Math.Max(DateTime.ParseExact(o["EndTime"].ToString(), "HHmm", provider).Hour, DateTime.ParseExact(p["EndTime"].ToString(), "HHmm", provider).Hour),

       Ratio =Math.Round(decimal.Parse(p["HPPS"].ToString()) + decimal.Parse(o["HPPS"].ToString()), 2)

    }).OrderBy(x => x.StartTime).Distinct().ToList();




Output I am Getting is
StartTime EndTime  duration  ratio
   7	    15       8	      3.98
  11        19       8        3.98
  15	    23	     8	      3.98
Posted
Updated 17-Sep-14 4:19am
v2

1 solution

Try this:
C#
var qry = from a in dt.AsEnumerable()
        join b in dt.AsEnumerable() on a.Field<int>("EndTime") equals b.Field<int>("StartTime")
        select new {
            StartTime = a.Field<int>("StartTime"),
            EndTime = b.Field<int>("EndTime"),
            Duration = a.Field<int>("Duration") + b.Field<int>("Duration"),
            Ratio = a.Field<double>("Ratio") + b.Field<double>("Ratio")
            };


Returns:

StartTime EndTime Duration Ratio
7         15      8        3.98 
11        19      8        3.98 
15        23      8        3.98 
 
Share this answer
 

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