寻找免费住宿

时间:2009-06-08 10:46:49

标签: entity-framework datetime sql-server-2005 entity-sql

我需要查询帮助? 我有2张桌子预订&单位等。 表保留具有列ResId,rfrom(datetime),rto(datetime),status(int),UnitID(外键)。 状态2表示已确认。 我需要在请求的时间段内获得所有免费单位,查询需要仅返回在请求期间内未确认预订(状态== 2)的单位(不存在)。 我正在使用实体框架所以它应该是eSQL查询(其他选项是使用存储过程,但我想避免这种情况)。数据库是sql express 2005。 此外,查询应根据表单位的值过滤单位,但这不是问题。 我可以使用linq查询结果(多个where语句)。

编辑: 此查询有效:


    select * from Units where
    not exists (select *
        from Reservations
        where Reservations.unitID = Units.unitID
        and Reservations.status = 2
        and (@datefrom between Reservations.rfrom and Reservations.rto-1
        or @dateto between Reservations.rfrom+1 and Reservations.rto
        or rfrom between @datefrom and @dateto-1 
        or rto between @datefrom+1 and @dateto))
and Units.category=@cat

怎么看实体sql?我可以用linq吗? 实体名称与表格相同。

2 个答案:

答案 0 :(得分:0)

select value u from AEDMEntities.Units as u WHERE
not exists (select value r from AEDMEntities.Reservations as r
where  r.Unit.unitID = u.unitID and r.status = 2 AND 
(datetime '2009-7-7 00:00'between r.rfrom and r.rto 
or datetime '2009-7-7 00:00' between r.rfrom and r.rto 
or r.rfrom between datetime '2009-7-7 00:00'and datetime '2009-7-27 00:00' 
or r.rto between datetime '2009-7-7 00:00' and datetime '2009-7-27 00:00'))
AND u.category=3

这很有效。 但我不能写-1或+1来减去/添加像sql一样的日子!如何实现这一点,我只需要进行参数化。单位是抽象类,它有2个派生类app& rooms(每个类型继承的表)所以我需要只返回应用程序或房间,取决于方法输入参数任何想法欢迎。

答案 1 :(得分:0)

我最终使用存储过程......