Nhibernate QueryOver如何让这个查询使用异步?

时间:2018-04-28 12:52:55

标签: asp.net asp.net-mvc asynchronous nhibernate queryover

对于这个新网站,我想在NHibernate中使用异步方法。我使用QueryOver API进行了这个简单的查询,但是我无法使用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方法一起使用。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

这个问题的解决方案是:

var result = await q.Select(
                Projections.Distinct(
                    Projections.Property<Business>(x => x.Id).WithAlias(() => bli.Id)
            )
            .TransformUsing(Transformers.AliasToBean<BusinessListItem>())
            .OrderBy(x => x.Name).Asc
            .Skip(_skipCount)
            .Take(_takeCount)
            .ListAsync<BusinessListItem>();

        return result.ToList();

致@DavidOsborne