Criteria is a simplified API for retrieving entities by composing Expression objects.

Namespace: NHibernate
Assembly: NHibernate (in NHibernate.dll) Version: 3.2.0.4000 (3.2.0.4000)

Syntax

C#
public interface ICriteria : ICloneable
Visual Basic
Public Interface ICriteria _
	Inherits ICloneable
Visual C++
public interface class ICriteria : ICloneable

Remarks

Using criteria is a very convenient approach for functionality like "search" screens where there is a variable number of conditions to be placed upon the result set.

The Session is a factory for ICriteria. Expression instances are usually obtained via the factory methods on Expression. eg:

CopyC#
IList cats = session.CreateCriteria(typeof(Cat))
    .Add(Expression.Like("name", "Iz%"))
    .Add(Expression.Gt("weight", minWeight))
    .AddOrder(Order.Asc("age"))
    .List();
You may navigate associations using CreateAlias(String, String) or CreateCriteria(String). eg:
CopyC#
IList<Cat> cats = session.CreateCriteria<Cat>
    .CreateCriteria("kittens")
    .Add(Expression.like("name", "Iz%"))
    .List<Cat>();

You may specify projection and aggregation using Projection instances obtained via the factory methods on Projections. eg:

CopyC#
IList<Cat> cats = session.CreateCriteria<Cat>
    .SetProjection(
        Projections.ProjectionList()
            .Add(Projections.RowCount())
            .Add(Projections.Avg("weight"))
            .Add(Projections.Max("weight"))
            .Add(Projections.Min("weight"))
            .Add(Projections.GroupProperty("color")))
    .AddOrder(Order.Asc("color"))
    .List<Cat>();

See Also