关于数据表帮助的LINQ查询 [英] LINQ query on Datatable Help

查看:64
本文介绍了关于数据表帮助的LINQ查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataTable



 id StartTime EndTime持续时间比率
1 7 11 4 1.99
2 11 15 4 1.99
3 15 19 4 1.99
4 19 23 4 1.99





需要输出如下

StartTime EndTime持续时间比率
7 15 8 3.98
15 23 8 3.98





我试过的是



来自 p   DataTable.AsEnumerable()
join o DataTable.AsEnumerable()

p [ EndTime]。ToString()等于o [ StartTime]。ToString()

orderby (Dat eTime.ParseExact(p [ StartTime]。ToString(), HHmm,provider))。小时

其中​​ int .Parse(p [ 持续时间]。ToString())+ int .Parse(o [ 持续时间]。ToString()))== 8

选择 new
{
ID = p [ ID]。ToString(),

StartTime = Math.Min(DateTime.ParseExact(p [ StartTime]。ToString(), HHMM ,提供者).Hour,DateTime.ParseExact(o [ StartTime]。ToString(),< span class =code-string> HHmm,provider).Hour),

EndTime = Math.Max (DateTime.ParseExact(o [ EndTime]。ToString(), HHmm,provider)。hour,DateTime.ParseExact(p [ EndTime]。ToString(), HHmm ,提供者)。小时),

比率= Math.Round( decimal .Parse(p [ HPPS]。ToString())+ decimal 。解析(o [ HPPS]。ToString()), 2

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









我得到的输出是

 StartTime EndTime持续时间比率
7 15 8 3.98
11 19 8 3.98
15 23 8 3.98

解决方案

试试这个:

  var  qry = 来自   dt.AsEnumerable()
join b dt.AsEnumerable()on a.Field< int>( EndTime)等于b.Field< int>( StartTime
选择 new {
StartTime = a.Field< int>( StartTime),
EndTime = b.Field< int>( EndTime),
持续时间= a .Field< int>( Duration)+ b.Field< int>( 持续时间),
比率= a.Field< double>( 比率)+ b.Field< double>( 比率
};





返回:



 StartTime EndTime持续时间比率
7 15 8 3.98
11 19 8 3.98
15 23 8 3.98


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

(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

解决方案

Try this:

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 


这篇关于关于数据表帮助的LINQ查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆