【发布时间】:2018-04-28 12:52:54
【问题描述】:
对于这个新网站,我想在 NHibernate 中使用异步方法。我使用 QueryOver API 进行了这个简单的查询,但我无法让这个查询与异步一起使用。
这是一个简单的查询,其中包含一些列出所有业务的 where 子句。每次执行此操作时,我都想要其中的 20 个。
查询:
BusinessListItem bli = null;
BusinessCategory bc = null;
Category c = null;
BusinessImage bi = null;
Image i = null;
var q = Session.QueryOver<Business>()
.JoinAlias(x => x.Categories, () => bc)
.JoinAlias(() => bc.Category, () => c)
.JoinAlias(x => x.Images, () => bi, JoinType.LeftOuterJoin)
.JoinAlias(() => bi.Image, () => i, JoinType.LeftOuterJoin)
.Where(() => bc.IsMain);
if (!string.IsNullOrEmpty(_name))
q.WhereRestrictionOn(x => x.Name).IsLike($"%{_name}%");
if (!string.IsNullOrEmpty(_streetName))
q.WhereRestrictionOn(x => x.StreetName).IsLike($"%{_streetName}%");
if (_categoryId != null)
q.Where(() => c.Id == _categoryId.Value);
if (_subCategoryIds != null)
q.WhereRestrictionOn(() => c.Id).IsIn(_subCategoryIds);
return q.Select(
Projections.Property<Business>(x => x.Id).WithAlias(() => bli.Id),
Projections.Property<Business>(x => x.Name).WithAlias(() => bli.Name),
Projections.Property("c.Name").WithAlias(() => bli.CategoryName),
Projections.Property("bi.Image").WithAlias(() => bli.Image)
)
.TransformUsing(Transformers.AliasToBean<BusinessListItem>())
.List<BusinessListItem>()
.OrderBy(x => x.Name)
.Skip(_skipCount)
.Take(20)
.ToList();
我知道 .ListAsync() 方法存在,但我无法将它与 Skip、Take 和 OrderBy 方法一起使用。
非常感谢任何帮助!
【问题讨论】:
-
为什么在实现结果之前不考虑排序和行限制?如果您这样做,您应该能够使用
ListAsync()方法并获得一个可能更有效的查询。 -
@DavidOsborne 你是什么意思,实现结果?我不明白你在说什么。
-
当您调用
List()时,NH 将接受您的查询,构建 SQL,然后运行它。如果您随后在返回的结果上使用 LINQ 或其他扩展方法,您将无法获得 RDBMS 可以提供的好处。当然,有时这是故意的。
标签: asp.net asp.net-mvc asynchronous nhibernate queryover