2010-08-11 52 views
4

目前我能得到這個SetResultTransformer方法返回一些任意類型的DTO的列表如下:ICriteria可以返回一個IDictionary而不是一個列表<DTO>?

var result = _session.CreateCriteria<Company>() 
    .Add(Restrictions.In(groupCompanyInfo, (int[])groups.Select(xx => xx.Id).ToArray())) 
    .SetProjection(Projections.ProjectionList() 
     .Add(Projections.GroupProperty(groupCompanyInfo), "CompanyInfoGroupID") 
     .Add(Projections.RowCount(), "TotalNumberOfCompanies")) 
    .SetResultTransformer(Transformers.AliasToBean<SomeDTO>()) 
    .List<SomeDTO>(); 

,其中作爲SomeDTO定義:

public class SomeDTO 
{ 
    public int GroupId { get; set; } 
    public int CountOfCompaniesInGroup { get; set; } 
} 

我認爲這是一個有點矯枉過正必須專門創建一個類型來從這個查詢中提取數據。理想情況下,我可以使用IDictionary<int,int>,因爲內置於框架中。儘管如此,似乎我可以返回一個List。

我以爲我可以拋出一個偷偷摸摸KeyValuePair<int,int>SetResultsTransformer,就像這樣:

var result = _session.CreateCriteria<Company>() 
    .Add(Restrictions.In(groupCompanyInfo, (int[])groups.Select(xx => xx.Id).ToArray())) 
    .SetProjection(Projections.ProjectionList() 
     .Add(Projections.GroupProperty(groupCompanyInfo)) 
     .Add(Projections.RowCount())) // note, I removed the aliases 
    .SetResultTransformer(Transformers.AliasToBean<KeyValuePair<int, int>>()) 
    .List<KeyValuePair<int, int>>(); 

result只是一個空KeyValuePair。我有沒有辦法做到這一點,還是我需要DTO?

回答

5

使用客戶端Linq投影。我做這一切的時候:

var result = _session.CreateCriteria... 
      .List<object[]> 
      .ToDictionary(x => (int)x[0], x => (int)x[1]); 
0
var result = _session.CreateCriteria<Company>() 
    .Add(Restrictions.In(groupCompanyInfo, (int[])groups.Select(xx => xx.Id).ToArray())) 
    .SetProjection(Projections.ProjectionList() 
     .Add(Projections.GroupProperty(groupCompanyInfo)) 
     .Add(Projections.RowCount())) // note, I removed the aliases 
    .List(); 

這應返回IList<object[]> :)

相關問題