【问题标题】:How to use NHibernate Projections to retrieve a collection如何使用 NHibernate Projections 检索集合
【发布时间】:2009-07-28 21:37:08
【问题描述】:

我懒加载集合,而且由于 person 表中有很多字段,我正在编写一个投影函数来仅检索某些属性。它适用于属性,而不是其他实体的集合。如果它们被加载为代理我会很好,我可以稍后再获取它们,但现在它只是加载为空。

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;
        }

【问题讨论】:

    标签: c# linq nhibernate lazy-loading projection


    【解决方案1】:

    尝试使用 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>();
    

    【讨论】:

      【解决方案2】:

      你有多少房产?我在客户端实体上可能有大约 30 个,并且在 NH 中加载它时没有问题。

      您可能会担心实际情况并非如此。 (老人 : 过早的优化是万恶之源” :) )

      话虽如此 - 我怀疑这样的东西是否受支持。

      【讨论】:

      • 我有大约 80 个属性,所以在这种情况下确实有必要。我很确定我不能用这段代码检索它,但我认为有一些方法可以通过投影来检索许多集合中的一个。
      • 在这种情况下,您可能会在 google 群组的 nhusers 群组中获得更多运气
      • 我已经把它贴在那里了。如果我得到答案,我会在这里发布。谢谢。
      • 为了记录,我的桌子有 50 个道具,所以是的,你打败了我 :) - 但它仍然不是问题。除非你有一些 TEXT 列,否则我不会担心。
      • 好吧,由于某种原因,查询需要大约 3 秒来加载大约 8 条记录,即使我已将所有集合设置为延迟加载。
      猜你喜欢
      • 1970-01-01
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多