【问题标题】:NHibernate QueryOver get time from datetime propertyNHibernate QueryOver 从 datetime 属性中获取时间
【发布时间】:2016-07-12 13:47:05
【问题描述】:

我在我的 SQL Server 数据库中将日期和时间都保存为DateTime,并且我的映射中有一个DateTime 属性。在我的查询中,我需要单独访问日期和时间。使用日期我没有问题,但是当尝试从 DateTime 获取时间时,我收到以下错误:

附加信息:无法解析属性:TimeOfDay of:IWS.DataContracts.ServicesPlanning

在其他属性中节省时间不是我想要的,即使我尝试过也无法让它发挥作用。

使用QueryOver查询:

Session.QueryOver<ServicesPlanning>()
    .JoinAlias(x => x.TeamMember, () => stm)
    .Where(() => stm.Id == _memberId)
    .Where(x => (x.AllDay && (x.StartDate == _startDate && x.EndDate == _endDate)) ||
        (!x.AllDay && x.StartDate.Date == _startDate && x.EndDate.Date == _endDate 
                && (x.StartDate.TimeOfDay > _endTime.Value || x.EndDate.TimeOfDay < _startTime.Value)))
    .RowCount() > 0;

使用 LINQ 查询:

return Session
        .Query<ServicesPlanning>()
        .Where(x => x.TeamMember.Id == _memberId)
        .Any(x => (x.AllDay && (x.StartDate.Date == _startDate && x.EndDate.Date == _endDate)) ||
        (!x.AllDay && x.StartDate.Date == _startDate && x.EndDate.Date == _endDate && (x.StartDate.TimeOfDay > _endTime.Value || x.EndDate.TimeOfDay < _startTime.Value)));

查询目的:检查是否已经有与此日期或时间的计划,以免我们有重叠。

非常感谢任何帮助!

【问题讨论】:

  • 你试过用 LINQ 代替 QueryOver 吗?
  • @PhilDegenhardt 是的,但我似乎遇到了同样的错误。我也会在上面发布我的 LINQ 查询。

标签: c# asp.net nhibernate nhibernate-mapping queryover


【解决方案1】:

与其尝试操纵数据库中的内容进行比较,不如调整参数?

如果您的数据库包含例如2016-01-01T08:00:00 作为日期,并且您想查询日期为2016-01-01 的记录而不管时间,您可以查询“介于”2016-01-01T00:00:002016-01-01T23:59:59 之间的日期,如下所示:

.Where(x => (x.AllDay && (x.StartDate == _startDate && x.EndDate == _endDate)) ||
    (!x.AllDay && x.StartDate >= _startDate.Date && x.StartDate < _startDate.Date.AddDays(1))... 

【讨论】:

  • 我认为,NH 是目前唯一的选择。 NH 需要被告知如何将 DateTime.TimeOfDay 属性转换为它的 SQL 等价物(如果有的话)。
  • 但问题是我需要按时检查。它是一个简单的日历,所以我的验证是我不能同时有 2 个事件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多