简体   繁体   English

带有 QueryOver 的子查询

[英]Subqueries with QueryOver

I have a issue in using subquery with queryover.我在使用带有 queryover 的子查询时遇到问题。

This is what I have这就是我所拥有的

      var address = QueryOver.Of<Address>()
            .Where(x => x.City.IsLike("%" + city + "%")).Select(x => x.Person.Id);

        var result = Session.QueryOver<Person>()
            .Where(x => x.Type.IsLike(type + "%"))
            .And(x => x.Name.IsLike("%" + name + "%"))
            .WithSubquery.WhereExists(address);

I have a table for Person and a person has multiple addreses.我有一张 Person 表,一个人有多个地址。

So Person id, name, type所以 Person id, name, type

and Address will have PersonId and city etc.和 Address 将有 PersonId 和 city 等。

So want to search a person by name and type as well as City which is in Address table所以想按姓名和类型以及地址表中的城市搜索一个人

Try something like this:尝试这样的事情:

Address address = null;
Person person = null;
var addressSubQuery = QueryOver.Of<Address>(() => address)
    .Where(Restrictions.EqProperty(Projections.Property(() => address.Person.Id), Projections.Property(() => person.Id)))
    .Where(() => address.City.IsLike("%" + city + "%"));

    var result = Session.QueryOver<Person>(() => person)
        .Where(x => x.Type.IsLike(type + "%"))
        .And(x => x.Name.IsLike("%" + name + "%"))
        .WithSubquery.WhereExists(addressSubQuery);

You need to use QueryOver's version of aliasing.您需要使用 QueryOver 的别名版本。 This way you can reference the Person element from other queries which you will eventually link into your main query.通过这种方式,您可以从其他查询中引用 Person 元素,这些查询最终将链接到您的主查询中。

This is the same as doing something like the following这与执行以下操作相同

Select * from Person
Where 
    Type like '%foo%'
    and Name like '%bar%'
    and exists ( select Id from Address 
                 where 
                      Address.PersonId = Person.Id
                      and Address.City like '%bar%' )

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

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