简体   繁体   English

NHibernate QueryOver从datetime属性获取时间

[英]NHibernate QueryOver get time from datetime property

I'm saving both the date and the time as DateTime in my SQL Server database, and I have a DateTime property in my mappings. 我在SQL Server数据库中将日期和时间都保存为DateTime ,并且在映射中具有DateTime属性。 In my query I need to access the date individually and the time as well. 在我的查询中,我需要分别访问日期和时间。 With date I have no problem but when trying to get the time from DateTime I get following error: 使用日期我没有问题,但是当尝试从DateTime获取时间时,出现以下错误:

Additional information: could not resolve property: TimeOfDay of: IWS.DataContracts.ServicesPlanning 附加信息:无法解析属性:TimeOfDay,属于:IWS.DataContracts.ServicesPlanning

Saving the time in a other property is not what I want even though I tried it and can't get it working either. 节省时间在其他属性中并不是我想要的,即使我尝试过并且也无法使其正常工作。

Query with QueryOver : 使用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;

Query with LINQ: 用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)));

Purpose of query: check if there already is a planning with this date or time so we don't have overlaps. 查询的目的:检查是否已经计划了这个日期或时间,所以我们没有重叠。

Any help is much appreciated! 任何帮助深表感谢!

Instead of trying to manipulate what's in the database for comparison, why don't you adjust the parameter? 为何不调整参数,而不是尝试操纵数据库中的内容进行比较?

If your database contains for example 2016-01-01T08:00:00 as date, and you want to query records with a date on 2016-01-01 regardless of time, you can query date "between" 2016-01-01T00:00:00 and 2016-01-01T23:59:59 , like so: 如果您的数据库包含例如2016-01-01T08:00:00作为日期,并且您想查询日期为2016-01-01记录(无论时间如何),则可以查询“ 2016-01-01T00:00:00 ”之间的日期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))... 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM