如何使用 nHibernate 和 QueryOver API 获得不同的结果? [英] How to get a distinct result with nHibernate and QueryOver API?
问题描述
我有这个 Repository 方法
I have this Repository method
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);
}
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();
}
您提供一个自由文本搜索字符串和一个标签列表.问题是,如果一条消息有多个标签,则它会被重复列出.我想要一个基于 Message 实体的不同结果.我看过
You supply a free text search string and a list of Tags. The problem is that if a message has more then one tag it is listed duplicated times. I want a distinct result based on the Message entity. I've looked at
Projections.Distinct
但它需要一个属性列表来解决不同的问题.此消息是我的实体根,最有可能在不提供所有实体属性的情况下获得此行为吗?
But it requires a list of Properties to to the distinct question on. This Message is my entity root there most be a way of getting this behaviour without supplying all of the entity properties?
提前致谢,安德斯
推荐答案
如果您使用的是 ICriteria API,您需要:
If you're using the ICriteria API, you need:
.SetResultTransformer(new DistinctEntityRootTransformer())
如果您使用 QueryOver API,您需要:
If you're using the QueryOver API, you need:
.TransformUsing(Transformers.DistinctRootEntity)
但请注意,这一切都发生在客户端,因此所有重复的行仍会被拉取.
But beware, this all occurs on client side, so all the duplicate rows are still pulled.
这篇关于如何使用 nHibernate 和 QueryOver API 获得不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!