2014-02-12 95 views
0

我有兩個班(實際上更多的)拆分查詢結果:NHibernate的 - 由子集合

public class City{ 
    public string Name; 
    public List<Street> Street; 
} 
public class Street{ 
    public string Name; 
    public City City; 
} 

我試圖使用過濾後的數據ICriterion大約像這樣中檢索:

ICriteria crit = CurrentSession 
       .CreateCriteria(typeof (City)); 
crit = crit.CreateAlias("Streets", "Street", NHibernate.SqlCommand.JoinType.LeftOuterJoin); 
crit.Add(Expression.InsensitiveLike("Name", "London", MatchMode.Exact)); 
crit.Add(Expression.InsensitiveLike("Street.Name", "Pic%", MatchMode.Exact)); 

我應該如何形成標準以獲得結果作爲僅包含一個元素的包含街道兒童集合的城市列表? 所以我需要得到一個結果:

City: 'London' -> Streets:{'Piccadilly'} 
City: 'London' -> Streets:{'Pickard'} 
City: 'London' -> Streets:{'Pickwick'} 

等 但此刻我得到:

City: 'London' -> Streets:{'Piccadilly', 'Pickard', 'Pickwick'} 
City: 'London' -> Streets:{'Piccadilly', 'Pickard', 'Pickwick'} 
City: 'London' -> Streets:{'Piccadilly', 'Pickard', 'Pickwick'} 

行數=發現街道的數量。我需要像'跨所有行的街道取消組合'

回答

0

我會勸阻使用城市作爲結果集,因爲它不會是一個有效的城市對象。

ICriteria crit = CurrentSession.CreateCriteria(typeof(City)) 
    .CreateAlias("Streets", "Street", JoinType.InnerJoin) 
    .Add(Expression.InsensitiveLike("Name", "London", MatchMode.Exact)) 
    .Add(Expression.InsensitiveLike("Street.Name", "Pic%", MatchMode.Exact)) 
    .SetProjection(Projections.List() 
     .Add(Projections.Property("Name")) 
     .Add(Projections.Property("Street.Name"))); 


var results1 = crit.List<object[]>() 
    .Select(arr => new LocationDto 
        { 
         CityName = (string)arr[0], 
         StreetName = (string)arr[1] 
        }); 

// or if it is really needed 
var results2 = crit.List<object[]>() 
    .Select(arr => new City 
        { 
         Name = (string)arr[0], 
         Streets = { new Street((string)arr[1]) } 
        });