2011-06-13 4 views
2

하위 쿼리를 queryover와 함께 사용하는 데 문제가 있습니다.QueryOver를 사용한 하위 쿼리

이 내가이 사람에 대한 테이블을 가지고 한 사람이 여러 addreses있다

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

을 것입니다.

그래서 사람 ID, 이름, 유형

및 주소 PersonId와 도시를 것 등

그래서

이름으로 사람을 검색하고 주소 테이블에있는 도시뿐만 아니라 입력합니다 이 같은

답변

6

시도 뭔가 :

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

당신은 QueryO를 사용할 필요가 ver의 에일리어싱 버전. 이렇게하면 주 쿼리로 연결되는 다른 쿼리에서 Person 요소를 참조 할 수 있습니다. 이것은 다음과 같은

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%') 
같은 일을하고 같은입니다