我有这个使用QueryOver API的Repository方法
public IList<Message> ListMessagesBy(string text,IList<Tag> tags,int pageIndex,out int count,out int pageSize) { pageSize = 10; var likeString = string.Format("%{0}%",text); var query = session.QueryOver<Message>() .Where(Restrictions.On<Message>(m => m.Text).IsLike(likeString) || Restrictions.On<Message>(m => m.Fullname).IsLike(likeString)); if (tags.Count > 0) { var tagIds = tags.Select(t => t.Id).ToList(); query .JoinQueryOver<Tag>(m => m.Tags) .WhereRestrictionOn(t => t.Id).IsInG(tagIds) .TransformUsing(Transformers.DistinctRootEntity); } count = 0; if(pageIndex < 0) { count = query.ToRowCountQuery().FutureValue<int>().Value; pageIndex = 0; } return query.OrderBy(m => m.Created).Desc.Skip(pageIndex * pageSize).Take(pageSize).List(); }
我试过了两个
.TransformUsing(Transformers.DistinctRootEntity);
和
.RootCriteria.SetResultTransformer(new DistinctEntityRootTransformer())
它搞砸了总计数(它返回结果无明显)和实际的分页(Skip / Take)
我怎样才能解决这个问题?
提前谢谢,安德斯
解决方法
尝试这样的事情
public IPagedList<Client> Find(int pageIndex,int pageSize) { Client clientAlias = null; var query = Session.QueryOver<Client>(() => clientAlias) .Select( Projections.Distinct( Projections.ProjectionList() .Add(Projections.Property<Client>(x => x.Id).As("Id")) .Add(Projections.Property<Client>(x => x.Name).As("Name")) .Add(Projections.Property<Client>(x => x.Surname).As("Surname")) .Add(Projections.Property<Client>(x => x.GivenName).As("GivenName")) .Add(Projections.Property<Client>(x => x.EmailAddress).As("EmailAddress")) .Add(Projections.Property<Client>(x => x.MobilePhone).As("MobilePhone")) ) ) .TransformUsing(Transformers.AliasToBean<Client>()) .OrderBy(() => clientAlias.Surname).Asc .ThenBy(() => clientAlias.GivenName).Asc; var count = query .ToRowCountQuery() .FutureValue<int>(); return query .Take(pageSize) .Skip(Pagination.FirstResult(pageIndex,pageSize)) .List<Client>() .ToPagedList(pageIndex,pageSize,count.Value); }