NHibernate - How to write a criteria query to reflect whether a column has a null value or not

StackOverflow https://stackoverflow.com/questions/11606417

Domanda

I am coding a screen which includes a list of items. Each item includes a button where the user can popup a dialog to enter or read (their previously entered) feedback on said line item. Because feedback contains a substantial amount of textual data and there may be many line items per page I am using AJAX to load feedback on demand.

I would like to have a different icon for the line item's feedback button depending on whether or not the user has left feedback. The goal is to make users aware of what is yet to be entered. I am using NHibernate as my ORM. Is it possible to write an NHibernate query that includes a boolean value indicating whether a database column is null or not? Otherwise is it possible to return the string length for each row, again using NHibernate? I'm using the Criteria API, but any help would be appreciated.

Essentially I am trying to do this:

SELECT id, name, has_feedback is null as has_preview FROM my_table;
È stato utile?

Soluzione

it is possible to get the string length using nhibernate functions

session.QueryOver<Foo>()
    .Select(Projections.SqlFunction("length", NHibernateUtil.Int32, Projections.Property<Foo>(foo => foo.Name)))
    .List();

session.CreateCriteria<Foo>()
    .SetProjection(Projections.SqlFunction("length", NHibernateUtil.Int32, Projections.Property(Name)))
    .List<int>();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top