NHibernate: QueryOver<> help

前端 未结 1 1969
庸人自扰
庸人自扰 2021-02-09 04:51

I\'m just starting out with NHibernate and I\'m having trouble with running more complex queries.

I have entities with a list of tags attached. The user will provide two

1条回答
  •  一生所求
    2021-02-09 05:46

    That query isn't trivial (have a think about how you might do this using raw SQL). I think the following will work (requiring two correlated sub-queries):

    
    Display displayAlias = null;
    
    var countIncludedTagsSubquery =
        QueryOver.Of()
            .Where(d => d.Id == displayAlias.Id)
            .JoinQueryOver(d => d.Tags)
                .WhereRestrictionOn(t => t.Id).IsInG(includedTagIds)
                .Select(Projections.RowCount());
    
    var excludedTagsSubquery =
        QueryOver.Of()
            .Where(d => d.Id == displayAlias.Id)
            .JoinQueryOver(d => d.Tags)
                .WhereRestrictionOn(t => t.Id).IsInG(excludedTagIds)
                .Select(t => t.Id);
    
    var displays =
        session.QueryOver(() => displayAlias)
            .WithSubquery.WhereValue(includedTagIds.Count).Eq(countIncludedTagsSubquery)
            .WithSubquery.WhereNotExists(excludedTagsSubquery)
            .List();
    

    0 讨论(0)
提交回复
热议问题