Fluent NHibernate Where Clause

Fluent NHibernate is a alternative Mapping framework to the default HBM files (Hibernate Mapping)

NHibernate itself offers different Querying API's like

  • HQL - Hibernate Querying Language
  • Criteria
  • Query - (LINQ Equivalent)
  • QueryOver - (strongly typed Criteria)
  • SQL

For Criteria your query would be something along the lines of:

var users = session.CreateCriteria<Users>()
                   .Add(Restrictions.Eq("UserName", "Abcd"))
                   .Add(Restrictions.Eq("Password", "123456"))
                   .List<Users>();

Query:

var users = from u in session.Query<Users>()
            where u.UserName == "Abcd"
            && u.Password == "123456"
            select u;

or

var users = session.Query<Users>()
                   .Where(x => x.UserName == "Abcd" && u.Password == "123456");

QueryOver:

var users = session.QueryOver<Users>()
                   .Where(x => x.UserName == "Abcd")
                   .And(u.Password == "123456")
                   .List();