简体   繁体   中英

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.

    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. 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 ?

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>();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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