Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6085307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:37:28+00:00 2026-05-23T11:37:28+00:00

I am trying to create an NHibernate criteria that will get me all posts

  • 0

I am trying to create an NHibernate criteria that will get me all posts that were created during a certain time of day.
For example, I want all posts that were created between the times of 12:15:00 and 2:20:00.

Post would look like this

public class Post
{
   public virtual DateTime CreatedOn { get; set;}
}

And the mapping:

<class name="Post" table="Posts">
   <property name="CreatedOn" not-null="true"/>
</class>

And I would like to use DetachedCriteria (this will be part of larger search)
something 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 that isn’t at all correct but shows what I am trying to accomplish.
Using SQL Server as the database.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:37:29+00:00Added an answer on May 23, 2026 at 11:37 am

    If you were to create the query directly in SQL you would use the datepart function. This question inquires about calling the datepart function from NHibernate. Basically you use the NHibernate projection: Projections.SqlFunction.

    What complicates things a little is that you would need to have multiple projections, one for the hour, minute, and second (depending on the granularity you need). Also, I haven’t found a way to call datepart directly since I couldn’t find a way to pass a datepart parameter (its a non-quoted string constant). Instead, you can extend the dialect like so:

      public class MsSql2008ExtendedDialect : MsSql2008Dialect
        {
            public MsSql2008ExtendedDialect()
            {
                RegisterFunction("DATEPART_HOUR", new SQLFunctionTemplate(NHibernateUtil.DateTime, "datepart(hh, ?1)"));
            }
        } 
    

    EDIT:
    I would caution however to take note of query performance. Since there is no index on the time of day, even if there is an index on the date column, query performance could suffer if there are many records and this is the only column being restricted on. In this case it might be better to add explicit columns for the hour/minute.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to NHibernate and trying to create my first mapping. I have created
I'm fairly new to NHibernate and I'm trying to modify a criteria that currently
I'm trying to create my first test application with NHibernate following example in NHibernate
Im trying to write an NHibernate criteria that effectively joins and restricts at the
While trying to create a Bi-directional one-to-one mapping in NHibernate, I found that, I
I am trying to create a Unidirectional one-to-one relationship using NHibernate. Example: An Order
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
I am trying to recreate something like the following SQL using NHibernate criteria: select
I'm using NHibernate 3.2 and I'm trying to create a projection with two columns
I am trying to create a custom application that allows for adding and removing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.