目前我可以使用此SetResultTransformer
方法返回一些任意类型的DTO的List,如下所示:
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>();
Run Code Online (Sandbox Code Playgroud)
其中SomeDTO定义为:
public class SomeDTO
{
public int GroupId { get; set; }
public int CountOfCompaniesInGroup { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我认为这有点过分需要创建一个专门用于从该查询中获取数据的类型.理想情况下,我可以使用a 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>>();
Run Code Online (Sandbox Code Playgroud)
但这result
只是一个空的KeyValuePair.我有什么方法可以做到这一点,还是我需要DTO?
使用客户端Linq投影.我一直这样做:
var result = _session.CreateCriteria...
.List<object[]>
.ToDictionary(x => (int)x[0], x => (int)x[1]);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
502 次 |
最近记录: |