LINQ子查询 - 自我加入

时间:2015-06-16 07:19:08

标签: c# sql-server linq

我附加了我的SQL查询:

select t1.vid,datediff(minute,MIN(t1.time),MAX(t2.time)) as WorkingTime 
    from 
    (select attendancetype as type,ls_vehicleid as vid,indianstdtime as time
    from db_vehicledailyattendance where CAST(Indianstdtime as DATE)='2015-06-06') t1
    join 
    (select attendancetype as type,ls_vehicleid as vid,indianstdtime as time 
    from db_vehicledailyattendance where CAST(Indianstdtime as DATE)='2015-06-06') t2 
    on t1.vid=t2.vid
    where t1.Type='Incoming' and t2.Type='Outgoing' group by t1.Type,t1.vid

我想将其转换为LINQ查询。基本上我的子查询选择出勤类型,可以是(传入,传出),车辆ID,时间。 我想要的是每辆车的(最大传出和最小传入)的差异,以便计算他们工作的小时数。

他们以任何方式制作临时桌子吗?我在SQL中使用的视图如下:

create view AttendanceSchedule(Type,Vid,Time) as 
select attendancetype,ls_vehicleid as vid,indianstdtime as time
from db_vehicledailyattendance 
where CAST(Indianstdtime as DATE)='2015-06-06'

使用此视图轻松获取数据。 我尝试过搜索但发现像DataSet和DataTables这样的东西,并没有真正理解如何在这里使用它们。

任何帮助将不胜感激。谢谢!

更新: 我现在又遇到了类似的问题。 我的MS SQL查询如下:

select * from
(select ls_vehicleid,MAX(indianstdtime) date,attendancetype from db_vehicledailyattendance
where CAST(indianstdtime as DATE)='2015-06-16' and attendancetype='Incoming' group by ls_vehicleid,attendancetype) a
full outer join
(select ls_vehicleid,MAX(indianstdtime) date,attendancetype from db_vehicledailyattendance
where CAST(indianstdtime as DATE)='2015-06-16' and attendancetype='Outgoing' group by ls_vehicleid,attendancetype) b
on a.ls_vehicleid=b.ls_vehicleid where a.date>b.date or b.date is null

这里是内部查询a:

select ls_vehicleid,MAX(indianstdtime) date,attendancetype from db_vehicledailyattendance
where CAST(indianstdtime as DATE)='2015-06-16' and attendancetype='Incoming' group by ls_vehicleid,attendancetype

为我提供最长入场时间的参赛作品。

内部查询b:

为我提供最长出场时间。

我的整个查询基本上给了我车辆最新作为'Incoming'。 如果我想在LINQ中使用它,我将不得不为a和b做一个临时表吗?或者是他们的另一种方式?我基本上在做这些的连接并从连接的结果中选择。

0 个答案:

没有答案