使用 nhibernate 进行动态排序

发布于 2024-08-01 20:51:18 字数 131 浏览 2 评论 0原文

假设您有一个名为 Articles 的表。

它有以下列: ID、名称、正文、创建、修改、浏览量等。

使用单一方法,是否可以提取文章列表,并将其传递给我想要排序的列? 还有方向,即 desc 或 asc。

Say you have a table named Articles.

It has the following columns:
ID, name, body, created, modified, pageviews, etc.

Using a single method, would it be possible to pull a list of articles, and pass it which column I want to order by? And also the direction, i.e. desc or asc.

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

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

发布评论

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

评论(1

那伤。 2024-08-08 20:51:18

我不确定“单一方法”注释是什么意思,但您可以使用 Criteria

IList articles = session.CreateCriteria(typeof(Article))
  .AddOrder( Order.Asc("Name") )
  .List();

显然,您可以将其包装在一个方法中,并将要排序的属性名称作为参数以及排序方向传递。 后者由 Order.Asc()Order.Desc() 指定。

I'm not sure what you mean by "single method" comment, but you can do this using Criteria:

IList articles = session.CreateCriteria(typeof(Article))
  .AddOrder( Order.Asc("Name") )
  .List();

Obviously, you can wrap this in a method and pass the name of the property you want to order by as parameter as well as sorting direction. The latter is specified by Order.Asc() or Order.Desc().

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