开发者

Fluent NHibernate Where Clause

I have to write a query in Fluent NHibernate f开发者_运维技巧or

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

How to create above query with session.CreateCriteria


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


Query Over

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


If you are using 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>(); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜