c# - NHibernate Criteria Search By Time Of Day -
i trying create nhibernate criteria me posts created during time of day. example, want posts created between times of 12:15:00 , 2:20:00.
post this
public class post { public virtual datetime createdon { get; set;} }
and mapping:
<class name="post" table="posts"> <property name="createdon" not-null="true"/> </class>
and use detachedcriteria (this part of larger search) similar to:
var criteria = detachedcriteria.for<post> .add(restriction.ge("createdon", time(12, 15, 00)) .add(restriction.le("createdon", time(2, 20, 00));
i know isn't @ correct shows trying accomplish. using sql server database.
if create query directly in sql use datepart function. this question inquires calling datepart function nhibernate. use nhibernate projection: projections.sqlfunction.
what complicates things little need have multiple projections, 1 hour, minute, , second (depending on granularity need). also, haven't found way call datepart directly since couldn't find way pass datepart parameter (its non-quoted string constant). instead, can extend dialect so:
public class mssql2008extendeddialect : mssql2008dialect { public mssql2008extendeddialect() { registerfunction("datepart_hour", new sqlfunctiontemplate(nhibernateutil.datetime, "datepart(hh, ?1)")); } }
edit: caution take note of query performance. since there no index on time of day, if there index on date column, query performance suffer if there many records , column being restricted on. in case might better add explicit columns hour/minute.
Comments
Post a Comment