NHibernate: QueryOver<> help

前端 未结 1 1975
庸人自扰
庸人自扰 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<Display>()
            .Where(d => d.Id == displayAlias.Id)
            .JoinQueryOver<DisplayTag>(d => d.Tags)
                .WhereRestrictionOn(t => t.Id).IsInG(includedTagIds)
                .Select(Projections.RowCount());
    
    var excludedTagsSubquery =
        QueryOver.Of<Display>()
            .Where(d => d.Id == displayAlias.Id)
            .JoinQueryOver<DisplayTag>(d => d.Tags)
                .WhereRestrictionOn(t => t.Id).IsInG(excludedTagIds)
                .Select(t => t.Id);
    
    var displays =
        session.QueryOver<Display>(() => displayAlias)
            .WithSubquery.WhereValue(includedTagIds.Count).Eq(countIncludedTagsSubquery)
            .WithSubquery.WhereNotExists(excludedTagsSubquery)
            .List();
    
    0 讨论(0)
提交回复
热议问题