如何使用NHibernate的预测来检索集合 [英] How to use NHibernate Projections to retrieve a collection

查看:104
本文介绍了如何使用NHibernate的预测来检索集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我懒加载的集合,也因为有这个人表中的许多领域,我写一个投影功能只检索某些属性。它的工作原理与性能,其他实体的只是没有集合。如果它们被装在作为代理,后来我能得到他们,但现在它只是加载在空我就可以了。

 公开的IList<&人GT; ListTop40()
{
无功名单= _session.CreateCriteria(typeof运算(人))
.SetProjection(Projections.ProjectionList()
。新增(Projections.Property(名字 ))
。新增(Projections.Property(名字))
。新增(Projections.Property(泽西))
。新增(Projections.Property(FortyYard))
。新增(Projections.Property(BenchReps))
。新增(Projections.Property(VertJump))
。新增(Projections.Property(ProShuttle))
。新增(Projections.Property(跳远踏板))
。新增(Projections.Property(PersonSchoolCollection))

&的.List LT; IList的>()
。选择(L =>新建人(){名字=(字符串)L [0],姓氏=(字符串)L [1],泽西=(十进制)L [2],FortyYard =(十进制)L [3 ],BenchReps =(十进制)L [4],VertJump =(十进制)L [5],ProShuttle =(十进制)L [6],跳远踏板=(十进制)L [7],PersonSchoolCollection =(IList的< Person_School>) L [8]});

&IList的LT;人> S = list.ToList();
返回小号;
}


解决方案

尝试使用AliasToBeanResultTransformer:

  VAR列表= _session.CreateCriteria(typeof运算(人))
.SetProjection(Projections.ProjectionList()
。新增(Projections.Property(名字))
。新增(Projections.Property(名字))
。新增(Projections.Property(泽西))
。新增(Projections.Property(FortyYard))
。新增(Projections.Property(BenchReps))
。新增(Projections.Property(VertJump))
。新增(预测.Property(ProShuttle))
。新增(Projections.Property(跳远踏板))
。新增(Projections.Property(PersonSchoolCollection))

。 SetResultTransformer(新NHibernate.Transform.AliasToBeanResultTransformer(typeof运算(人)))
&的.List LT;人>();


I am lazy loading the collections, and also because there are so many fields within the person table, I am writing a projection function to retrieve only certain properties. It works with properties, just not collections of other entities. I would be fine if they were loaded in as proxies and i could get them later, but right now it just loads in null.

public IList<Person> ListTop40()
        {
            var list = _session.CreateCriteria(typeof(Person))
                   .SetProjection(Projections.ProjectionList()
                   .Add(Projections.Property("FirstName"))
                   .Add(Projections.Property("LastName"))
                   .Add(Projections.Property("Jersey"))
                   .Add(Projections.Property("FortyYard"))
                   .Add(Projections.Property("BenchReps"))
                   .Add(Projections.Property("VertJump"))
                   .Add(Projections.Property("ProShuttle"))
                   .Add(Projections.Property("LongJump"))
                   .Add(Projections.Property("PersonSchoolCollection"))
                    )
                    .List<IList>()
                    .Select(l => new Person() { FirstName = (string)l[0], LastName = (string)l[1], Jersey = (Decimal)l[2], FortyYard = (Decimal)l[3], BenchReps = (Decimal)l[4], VertJump = (Decimal)l[5], ProShuttle = (Decimal)l[6], LongJump = (Decimal)l[7], PersonSchoolCollection = (IList<Person_School>)l[8]});

            IList<Person> s = list.ToList();
            return s;
        }

解决方案

try using AliasToBeanResultTransformer:

var list = _session.CreateCriteria(typeof(Person))
               .SetProjection(Projections.ProjectionList()
               .Add(Projections.Property("FirstName"))
               .Add(Projections.Property("LastName"))
               .Add(Projections.Property("Jersey"))
               .Add(Projections.Property("FortyYard"))
               .Add(Projections.Property("BenchReps"))
               .Add(Projections.Property("VertJump"))
               .Add(Projections.Property("ProShuttle"))
               .Add(Projections.Property("LongJump"))
               .Add(Projections.Property("PersonSchoolCollection"))
                )
               .SetResultTransformer(new NHibernate.Transform.AliasToBeanResultTransformer(typeof(Person)))
               .List<Person>();

这篇关于如何使用NHibernate的预测来检索集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆