【问题标题】:Searching free accomodation寻找免费住宿
【发布时间】:2009-06-08 10:46:49
【问题描述】:

我需要查询方面的帮助吗? 我有 2 张桌子预订和其他单位。 表保留有列 ResId,rfrom(datetime),rto(datetime),status(int),UnitID(foreign key)。 状态 2 表示已确认。 我需要在请求期间获取所有免费单位,查询需要仅返回在请求期间(不存在)未确认预订(状态 ==2)的单位。 我正在使用实体框架,所以它应该是 eSQL 查询(其他选项是使用存储过程,但我想避免这种情况)。数据库是 sql express 2005。 查询还应根据表单元中的值过滤单元,但这不是问题。 我可以在查询结果(多个 where 语句)上使用 linq 来做到这一点。

编辑: 此查询有效:


    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 做吗? 实体名称与表相同。

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:
      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
      

      这是有效的。 但我不能像在 sql 中那样写 -1 或 +1 来减/加天! 如何实现这一点,我只需要将其参数化。 Unit 是抽象类,它有 2 个派生类 app&rooms(每个类型继承的表),所以我只需要根据方法输入参数返回应用程序或房间,欢迎提出任何想法。

      【讨论】:

        猜你喜欢
        • 2011-08-27
        • 2013-09-27
        • 1970-01-01
        • 2018-04-26
        • 2012-02-29
        • 1970-01-01
        • 2015-06-04
        • 2014-01-21
        相关资源
        最近更新 更多