Questions tagged [nhibernate-criteria]

The NHibernate Criteria API allows performing dynamic, object oriented queries in NHibernate.

Using the criteria API is one way to perform queries in NHibernate. Its strength is on writing dynamic queries at runtime. It lacks some features compared to HQL, for instance creation of cross products and joining objects on arbitrary properties.

Related:

  • QueryOver is a successor of Criteria, which provides more type safety at compile time, but is still base on Criteria.
  • Linq To NHibernate once based on Criteria, but had been rewritten to use HQL in later versions.

References:

286 questions
5
votes
1 answer

Execute nHibernate icriteria on result of other query (two distinct queries)

Is there a way to use an ICriteria result as a 'base' for a subsequent criteria query? For example if I would like to create a query SELECT department_id, sum(cost) AS total FROM payment GROUP BY payment.department_id storing the result as query0,…
Dani
  • 2,602
  • 2
  • 23
  • 27
5
votes
2 answers

NHibernate QueryOver restrict by string length

How do I restrict a query by the length of a string property? eg. something like: NHSession.QueryOver() .Where(p => p.RegistryCode.Length == 8)
dänjel
  • 53
  • 4
5
votes
1 answer

NHibernate: How to select the root entity in a projection

Ayende describes a really great way to get page count, and a specific page of data in a single query here: http://ayende.com/blog/2334/paged-data-count-with-nhibernate-the-really-easy-way His method looks like: IList list =…
5
votes
2 answers

How to create criteria query for given sql query

I am creating an ICriteria query for this equivalent sql query. SELECT fCustomerID, ISNULL( (SELECT SUM(payinv.fAmount) AS Expr1 FROM dbo.tARPayment AS pay INNER JOIN dbo.tARPaymentInvoice AS…
5
votes
1 answer

Multiple conditions in HAVING clause with NHibernate Criteria?

I'm trying to use NHibernate's Criteria API to write the equivalent of this: select foo_id from foo_history group by foo_id having sum(bar_in) > 0 or sum(baz_in) > 0; with this mapping:
wes
  • 1,577
  • 1
  • 14
  • 32
4
votes
3 answers

How to query a foreign key column with NHibernate, without retrieving the related entity

Say I have two classes: Parent and Child. A Parent has a property Children, which is of course a collection of Child objects. Child doesn't have a ParentId property. It does have a Parent property. So, my NHibernate mapping for Child…
Peter
  • 13,733
  • 11
  • 75
  • 122
4
votes
2 answers

Fluent NHibernate and filtering one-to-many relationship on query requiring multiple joins?

I recently got started with NHibernate and am having some trouble implementing the domain model outlined further down. What I'm looking for is a way to filter the relationship between an Item and it's ItemData collection on specific DataStores.…
4
votes
1 answer

Can I use NHibernate Criteria to project an entity and its child collection onto a class?

I'm using NH Criteria to retrieve an entity and project selective fields onto a custom class (a bit like projecting data onto a ViewModel for display on an MVC view). This is easy enough using ProjectionList: var emailCriteria =…
4
votes
2 answers

Composite Id's and Restrictions.IdEq or with comparison in Linq not working as expected

I have an entity where a composite id is used. I changed to code to make use of wrapping the composite id in a seperate key class. I expected that with Linq I could do a comparison on key object and with the Criteria API to use Restrictions.IdEq but…
Ramon Smits
  • 2,482
  • 1
  • 18
  • 20
4
votes
1 answer

Fluent Nhibernate - selecting specific column and count query with group by

I'm having some trouble excuting a query in fluent nhibernate. I have a table : Books with the following columns: ID, NAME, YEAR, BOOK_TYPE, AUTHOR_ID I want to excute the following sql query in Fluent NHibernate: SELECT BOOK_TYPE, COUNT(*) FROM…
4
votes
1 answer

Nhibernate count distinct (based on multiple columns)

Basically, i have been trying to do this (count distinct based on two columns): select count(distinct(checksum(TableA.PropertyA, TableB.PropertyB))) from TableA left outer join TableB on TableA.TableBId = TableB.Id where PropertyA like…
raberana
  • 11,739
  • 18
  • 69
  • 95
4
votes
2 answers

How can QueryOver be used to filter for a specific class?

I am currently dynamically constructing queries like so: QueryOver q = QueryOver.Of(); if (foo != null) q = q.Where(b => b.Foo == foo); // ... Now there are multiple mapped subclasses of Base (e. g. Derived) which I want to…
Thomas Luzat
  • 710
  • 7
  • 21
4
votes
0 answers

Select specific columns from subquery in Nhibernate Criteria

How Can I translate the following SQL query to Criteria query: Select A.TextProperty, B.TextProperty, Count(C.Id) From A, B, C Where A.Id In ( Select A.Id, C.Id, Count(C.Id) From A, C Having Count(C.Id) > 1 ) I know the Where part is…
Ruba
  • 867
  • 3
  • 11
  • 19
4
votes
1 answer

How to do a one-to-one relationship with null in NHibernate?

I have two NHibernate entities with a one-to-one relationship. Let's call them Dog and Owner. public class Dog { public virtual int Id { get;set; } public virtual Owner Owner { get; set; } } public class Owner { public virtual int Id…
4
votes
3 answers

nHibernate: Conjunctions and Disjunctions using QueryOver

Let's say I have a SQL query I need to render using nHibernate. The SQL query's WHERE clause consists of three OR statements, each of which contains a list of conditions. For example: SELECT * FROM MyTable WHERE (OrderId = 1 and ItemId =…
Mass Dot Net
  • 2,150
  • 9
  • 38
  • 50
1
2
3
19 20