流畅的 NHibernateWhere 子句

发布于 2024-11-19 23:02:10 字数 182 浏览 1 评论 0原文

我必须在 Fluent NHibernate 中编写一个查询

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

如何使用 session.CreateCriteria 创建上述查询

I have to write a query in Fluent NHibernate for

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

How to create above query with session.CreateCriteria

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

划一舟意中人 2024-11-26 23:02:10

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

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

  • HQL - Hibernate 查询语言
  • Criteria
  • Query - (LINQ 等效)
  • QueryOver - (强类型条件)
  • SQL

For Criteria 您的查询将是这样的沿着以下路线:

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;

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

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();
小红帽 2024-11-26 23:02:10

查询结束

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

Query Over

var users = session.QueryOver<Users>()
                   .Where(x => x.UserName == "Abcd" && x.Password == "123456")
                   .List();
相思故 2024-11-26 23:02:10

如果您使用 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>(); 

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>(); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文