流利的NHibernate Where Clause

时间:2011-07-12 04:10:02

标签: nhibernate fluent-nhibernate

我必须在Fluent NHibernate中为

编写一个查询

Select * from Users where UserName = 'Abcd' AND Password = '123456'

如何使用session.CreateCriteria

创建上述查询

3 个答案:

答案 0 :(得分:47)

Fluent NHibernate是默认HBM文件(Hibernate Mapping)的替代映射框架

NHibernate本身提供了不同的查询API,如

  • HQL - Hibernate查询语言
  • 标准
  • 查询 - (LINQ Equivalent)
  • QueryOver - (强类型标准)
  • SQL

对于Criteria,您的查询将是:

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

查询:

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

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();

答案 1 :(得分:0)

查询

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

答案 2 :(得分:0)

如果您使用的是IQuery:

User user = new User();
user.username="Abcd";
user.password="123456";

IQuery q = session.CreateQuery("from foo in class Foo where
user.username=:username and user.password=:password");
q.SetProperties(user);
var users= q.List<User>();