简体   繁体   English

Nhibernate QueryOver如何使该查询使用异步?

[英]Nhibernate QueryOver how can I make this query use async?

For this new website I want to use async methods in NHibernate. 对于这个新网站,我想在NHibernate中使用异步方法。 I have this simple query using QueryOver API but I can't get this one to work with async. 我有一个使用QueryOver API的简单查询,但是我无法使用异步查询。

It is a simple query with some where clauses that list all businesses. 这是一个简单的查询,其中包含列出所有业务的where子句。 I want 20 of them each time I execute this. 每次执行此操作时,我要20个。

Query: 查询:

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

I know the method .ListAsync() exists but I cannot get it working together with the Skip, Take and OrderBy method. 我知道方法.ListAsync()存在,但无法使其与Skip,Take和OrderBy方法一起使用。

Any help is much appreciated! 任何帮助深表感谢!

The solution to this question is : 这个问题的解决方案是:

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

Thx to @DavidOsborne 感谢@DavidOsborne

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM