ICriteria可以返回IDictionary而不是List< DTO>吗? [英] Can an ICriteria return an IDictionary instead of a List<DTO>?
问题描述
目前,我可以使用此SetResultTransformer
方法来返回某种DTO的列表,如下所示:
Currently I can get this SetResultTransformer
method to return a List of some arbitrary kind of DTO as follows:
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定义为:
where SomeDTO is defined as:
public class SomeDTO
{
public int GroupId { get; set; }
public int CountOfCompaniesInGroup { get; set; }
}
我认为必须专门创建一个类型以将数据从此查询中取出来,这有点过头了.理想情况下,因为可以内置在框架中,所以我可以使用IDictionary<int,int>
.照现在看来,我可以返回一个列表.
I think it's a little bit of an overkill having to create a type specifically to take the data out of this query though. Ideally, I could use a IDictionary<int,int>
, because built into the framework. As it stands though, it seems I can return a List.
我以为我可以像这样将一个偷偷摸摸的KeyValuePair<int,int>
扔到SetResultsTransformer
中:
I thought I could throw a sneaky KeyValuePair<int,int>
into the SetResultsTransformer
, like this:
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?
but result
is just an empty KeyValuePair. Is there any way for me to do this, or do I need the DTO?
推荐答案
使用客户端的Linq投影.我一直在这样做:
Use a client-side Linq projection. I do this all the time:
var result = _session.CreateCriteria...
.List<object[]>
.ToDictionary(x => (int)x[0], x => (int)x[1]);
这篇关于ICriteria可以返回IDictionary而不是List< DTO>吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!