使用 Join 和 Distinct 进行 QueryOver
我使用以下 QueryOver:
var query = searchTermRepository.GetAllOver()
.Where(Restrictions.On<Entities.SearchTerm>(c => c.Text).IsLike(filter.Value, MatchMode.Start))
.Select(Projections.Distinct(Projections.Property<Entities.SearchTerm>(x => x.Contact)))
.Inner.JoinQueryOver(x => x.Contact).Take(100);
这会创建:
SELECT distinct TOP ( 100 /* @p0 */ ) this_.ContactId as y0_
FROM SearchTerm this_
inner join Contact contact1_
on this_.ContactId = contact1_.Id
left outer join Company contact1_1_
on contact1_.Id = contact1_1_.Id
left outer join Person contact1_2_
on contact1_.Id = contact1_2_.Id
left outer join Branch contact1_3_
on contact1_.Id = contact1_3_.Id
left outer join ContactGroup contact1_4_
on contact1_.Id = contact1_4_.Id
WHERE this_.Text like 'koc%%' /* @p1 */
但我想
SELECT distinct TOP ( 100 /* @p0 */ ) this_.ContactId as y0_, contact1_.*
FROM SearchTerm this_
inner join Contact contact1_
on this_.ContactId = contact1_.Id
left outer join Company contact1_1_
on contact1_.Id = contact1_1_.Id
left outer join Person contact1_2_
on contact1_.Id = contact1_2_.Id
left outer join Branch contact1_3_
on contact1_.Id = contact1_3_.Id
left outer join ContactGroup contact1_4_
on contact1_.Id = contact1_4_.Id
WHERE this_.Text like 'koc%%' /* @p1 */
选择联系人的所有属性。
最好的问候,托马斯
I use the follow QueryOver:
var query = searchTermRepository.GetAllOver()
.Where(Restrictions.On<Entities.SearchTerm>(c => c.Text).IsLike(filter.Value, MatchMode.Start))
.Select(Projections.Distinct(Projections.Property<Entities.SearchTerm>(x => x.Contact)))
.Inner.JoinQueryOver(x => x.Contact).Take(100);
This creates:
SELECT distinct TOP ( 100 /* @p0 */ ) this_.ContactId as y0_
FROM SearchTerm this_
inner join Contact contact1_
on this_.ContactId = contact1_.Id
left outer join Company contact1_1_
on contact1_.Id = contact1_1_.Id
left outer join Person contact1_2_
on contact1_.Id = contact1_2_.Id
left outer join Branch contact1_3_
on contact1_.Id = contact1_3_.Id
left outer join ContactGroup contact1_4_
on contact1_.Id = contact1_4_.Id
WHERE this_.Text like 'koc%%' /* @p1 */
But I want
SELECT distinct TOP ( 100 /* @p0 */ ) this_.ContactId as y0_, contact1_.*
FROM SearchTerm this_
inner join Contact contact1_
on this_.ContactId = contact1_.Id
left outer join Company contact1_1_
on contact1_.Id = contact1_1_.Id
left outer join Person contact1_2_
on contact1_.Id = contact1_2_.Id
left outer join Branch contact1_3_
on contact1_.Id = contact1_3_.Id
left outer join ContactGroup contact1_4_
on contact1_.Id = contact1_4_.Id
WHERE this_.Text like 'koc%%' /* @p1 */
I want select all Properties of Contact.
Best Regards, Thomas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你没有投射接触的所有属性,但应该如此。我不知道是否有一种简写方式可以表示“联系上的所有属性”,或者您是否只需要一次执行一个,但现在您只是说“不同的前 100 个 searchterm.contactid”
You arent projecting all of the properties of contact and should be. I don't know if there is a short hand way to say "all of the properties on contact" or if you just need to do one at a time, but right now you are just saying "distinct top 100 searchterm.contactid"
这里列出的以前的代码对我没有帮助。
我也遇到了这个问题。首先,Distinct 确实有效,但只有在调用 QueryOver.List.ToList() 方法之后才有效,因此 query.skip 无法正常工作,对重复项进行分页,创建列表,然后由于重复项而减少我的分页量。
我发现做的最简单的事情是..首先创建一个唯一ID的列表,然后对ID本身进行分页..
然后在你的结果集上你可以简单地执行一个ID并仅在新分页的ID结果中检索ID放。
特别感谢.. https://julianjelfs.wordpress.com/2009/04/03/nhibernate-removing-duplicates-combined-with-paging/
Previous code here listed didn't help me..
I had a issue with this aswell. Firstly the Distinct does work, but only after the QueryOver.List.ToList() method was called, so the query.skip wouldn't work properly, paging over the duplicates, creating the list, then reducing my paged amount because of the duplicates.
Easiest thing i found to do was.. simply create a list of unique ids first, then do your pagination on the Ids themselves..
Then on your result set you can simply perform a Id and retrieve the ids only in your newly paginated id result set.
Special thanks to.. https://julianjelfs.wordpress.com/2009/04/03/nhibernate-removing-duplicates-combined-with-paging/
您必须明确指定要投影的所有列。据我所知,没有办法解决这个问题。
下面是一些使用 QueryOver 的快速代码:
然后您需要使用 AliasToBean 转换器将其转换为您的对象。
You do have to explicitly specify all of the columns that you want to project. There's no way around this that I know of.
Here's some quick code off the top of my head that uses QueryOver:
You'll then need to transform this to your object using the AliasToBean transformer.