2013-07-10 41 views
0

什麼是NHibernate的過濾器具有相同的結果,因爲這SQL查詢:NHibernate的過濾器進行自連接表

SELECT A.Id as ItemId, A.column1 as ItemTitle, A.ListId as ItemListId, 
B.Id as LookupItemId, B.Value as LookupItemValue, B.ListId as LookupItemListId 
FROM MyTable A LEFT JOIN MyTable B ON B.Id = A.LookupId  
WHERE A.ListId = 'EmployeeListGuid' 
AND (B.ListId='EmployeerListGuid' OR B.Id IS NULL)  

回答

0

首先,創建實體屬性的別名,這將我們的表LEFT JOIN自我:

criteria.CreateAlias("LookupItem", "B", JoinType.LeftOuterJoin); 

然後,我們可以用這個別名標準:

critera.Add(Restrictions.Eq("ListId", "EmployeeListGuid"));  
critera.Add(
    Restrictions 
     .Or(
     Restrictions.Eq("B.ListId", "EmployeerListGuid"), 
     Restrictions.IsNull("B.Id")));