关于Datatable的LINQ查询

时间:2014-09-17 14:47:48

标签: .net linq c#-4.0 linq-to-objects

我有一个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

我试过的是

`(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();'

我正在获得的输出


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

0 个答案:

没有答案