简体   繁体   English

nHibernate queryover复杂查询

[英]nHibernate queryover complex query

I've been battling this all day and can't seem to get the result I want. 我整天都在争吵,似乎无法得到我想要的结果。 I want to get all the nodes that has a certain tag in their tag collection. 我想获取其标签集合中具有特定标记的所有节点。

I'm using this QueryOver command but I'm getting all the possible nodes even though it looks to me as it should only be one. 我正在使用这个QueryOver命令,但我得到了所有可能的节点,即使它看起来像我应该只有一个。

    public IEnumerable<Node> GetAllWithTag(int tagId)
    {
        tags = new List<Tag>();
        Node node = null;
        Tag tagQ = null;

        var subQuery = QueryOver.Of<Tag>(() => tagQ)
            .Where(tag1 => tag1.Id == tagId).DetachedCriteria;

        subQuery = subQuery.SetProjection(Projections.Property("Id"));

        var nodes = _applicationUnitOfWork.GetSession().QueryOver<Node>(
            () => node).Where(Subqueries.Exists(subQuery)).List<Node>();

        return nodes;
    }

I am using this strange setup because I got a method error when I tried to use Contains on node.Tags. 我正在使用这个奇怪的设置,因为当我尝试在node.Tags上使用Contains时遇到方法错误。 So I read around a bit and it seems most everyone goes with a solution like this. 所以我读了一下,似乎大多数人都有这样的解决方案。 I just can't figure out where I'm doing something stupid. 我只是无法弄清楚我在做什么愚蠢的事情。 Most thankful for any help! 非常感谢任何帮助!

Have you tried using JoinQueryOver or JoinAlias ? 您是否尝试过使用JoinQueryOverJoinAlias

Something like this: 像这样的东西:

public IEnumerable<Node> GetAllWithTag(int tagId)
{
    return _applicationUnitOfWork.GetSession().QueryOver<Node>()
        .JoinQueryOver<Tag>(n => n.Tags)
            .Where(t => t.Id == tagId)
            .List<Node>();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM