【问题标题】:QueryOver with Join and Distinct带有 Join 和 Distinct 的 QueryOver
【发布时间】:2011-05-25 16:46:36
【问题描述】:

我使用如下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 */

我想选择联系人的所有属性。

最好的问候,托马斯

【问题讨论】:

    标签: nhibernate join distinct queryover


    【解决方案1】:

    您必须明确指定要投影的所有列。据我所知,没有办法解决这个问题。

    下面是一些使用 QueryOver 的快速代码:

    Contact contact = null;
    
    Session
    .QueryOver(() => contact)
    .SelectList(list => list
        .Select(Projections.Distinct(Projections.Property(x => x.Contact))) 
        .Select(c => c.Id).WithAlias(() => contact.Id)
        .Select(c => c.FirstName).WithAlias(() => contact.FirstName)
    ... and so on
    

    然后您需要使用 AliasToBean 转换器将其转换为您的对象。

    【讨论】:

    • 不要忘记对不同查询至关重要的“OrderBy”
    【解决方案2】:

    你并没有投射出接触的所有属性,而且应该如此。我不知道是否有一种简单的方法可以说“所有联系的属性”,或者您是否只需要一次做一个,但现在您只是说“不同的前 100 个 searchterm.contactid”

    【讨论】:

      【解决方案3】:

      此处列出的先前代码对我没有帮助..

      我也有这个问题。首先 Distinct 确实有效,但仅在调用 QueryOver.List.ToList() 方法之后,因此 query.skip 无法正常工作,对重复项进行分页,创建列表,然后由于重复项而减少我的分页量.

      我发现做的最简单的事情是.. 只需先创建一个唯一 ID 列表,然后对 ID 本身进行分页..

      然后在您的结果集上,您可以简单地执行一个 Id 并仅在新分页的 id 结果集中检索 id。

      //Create your query as usual.. apply criteria.. do what ever you want.
      
      //Get a unique set of ids from the result set.
      var idList = query.
      .Select(x => x.Id)
      .List<long>().Distinct().ToList();
      
      //Do your pagination here over those ids
      List<long> pagedIds = idList.Skip(0).Take(10).ToList();
      
      //Here what used to be non distinct resultset, now is..
      List<T> resultquery.Where(() => 
      item.Id.IsIn(pagedIds))
      .List<Person>()
      .ToList();
      

      特别感谢..https://julianjelfs.wordpress.com/2009/04/03/nhibernate-removing-duplicates-combined-with-paging/

      【讨论】:

        猜你喜欢
        • 2019-07-27
        • 2013-08-28
        • 2012-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-11
        • 1970-01-01
        相关资源
        最近更新 更多