How to get a distinct result with nHibernate and QueryOver API?

前端 未结 4 2092
悲&欢浪女
悲&欢浪女 2020-12-25 10:01

I have this Repository method

    public IList ListMessagesBy(string text, IList tags, int pageIndex, out int count, out int pageSi         


        
4条回答
  •  长情又很酷
    2020-12-25 10:26

    Try something like this

    public IPagedList Find(int pageIndex, int pageSize)
    {
        Client clientAlias = null;
    
        var query = Session.QueryOver(() => clientAlias)
    
            .Select(
                Projections.Distinct(
                    Projections.ProjectionList()
                        .Add(Projections.Property(x => x.Id).As("Id"))
                        .Add(Projections.Property(x => x.Name).As("Name"))
                        .Add(Projections.Property(x => x.Surname).As("Surname"))
                        .Add(Projections.Property(x => x.GivenName).As("GivenName"))
                        .Add(Projections.Property(x => x.EmailAddress).As("EmailAddress"))
                        .Add(Projections.Property(x => x.MobilePhone).As("MobilePhone"))
                )
            )
            .TransformUsing(Transformers.AliasToBean())
    
            .OrderBy(() => clientAlias.Surname).Asc
            .ThenBy(() => clientAlias.GivenName).Asc;
    
        var count = query
            .ToRowCountQuery()
            .FutureValue();
    
        return query
            .Take(pageSize)
            .Skip(Pagination.FirstResult(pageIndex, pageSize))
            .List()
            .ToPagedList(pageIndex, pageSize, count.Value);
    }
    

提交回复
热议问题