ICriteria可以返回IDictionary而不是List 吗?

目前我可以得到这个SetResultTransformer方法返回一些任意种类的DTO的List,如下所示:

 var result = _session.CreateCriteria() .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()) .List(); 

其中SomeDTO定义为:

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

我认为这有点过分需要创建一个专门用于从该查询中获取数据的类型。 理想情况下,我可以使用IDictionary ,因为内置于框架中。 尽管如此,似乎我可以返回一个List。

我以为我可以将偷偷摸摸的KeyValuePair扔进SetResultsTransformer ,如下所示:

 var result = _session.CreateCriteria() .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>()) .List<KeyValuePair>(); 

result只是一个空的KeyValuePair。 我有什么方法可以做到这一点,还是我需要DTO?

使用客户端Linq投影。 我一直这样做:

 var result = _session.CreateCriteria... .List .ToDictionary(x => (int)x[0], x => (int)x[1]); 
 var result = _session.CreateCriteria() .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 🙂