简体   繁体   English

NHibernate Projections(QueryOver.SelectList)限制

[英]NHibernate Projections (QueryOver.SelectList) limits

I'm new on stackoverflow and i hope this question will be appreciated. 我是stackoverflow的新手,我希望这个问题值得赞赏。

Simply said: I select everything from table x left outer join table y. 简单地说:我从表x左外连接表y中选择所有内容。 Table x has way too many columns so i make new object x. 表x有太多列,所以我创建新对象x。 This object is used for the Projection. 此对象用于投影。 I can project every single column of table xi want. 我可以预测表xi想要的每一列。 But when i try to project/select an collection (the collection of table y) then i get the same error: 'Index was outside the bounds of the array'. 但是当我尝试投影/选择一个集合(表y的集合)时,我得到了同样的错误:'索引超出了数组的范围'。

My question: Does NHibernate support to select/project a collection at all? 我的问题:NHibernate是否支持选择/投射一个集合? Because i've seen this question multiple times by searching on google (and stackoverflow), but none of those questions were answered. 因为我通过搜索谷歌(和stackoverflow)多次看到这个问题,但没有一个问题得到解答。

Code example: 代码示例:

public class X
{
    public virtual int ID { get; set; }
    public virtual int IDontNeedMoreInfoAboutClassXItTakesToMuchTimeToRetrieve { get; set; }
    public virtual IList<Y> YCollection { get; set; }
}

public class Y
{
    public virtual int YID { get; set; }
}

public class XRepository{
    public ISession Session {get; set;}
    public IList<X> Get()
    {

        X xAlias = null;
        Y yAlias = null;
        X resultAlias = null;
        return Session.QueryOver<X>()
            .JoinAlias(() => xAlias.YCollection, () => yAlias, JoinType.LeftOuterJoin)
            .SelectList(list => list
                .SelectGroup(() => xAlias.ID).WithAlias(() => resultAlias.ID)
                .SelectGroup(() => xAlias.YCollection).WithAlias(() => resultAlias.YCollection)) // Index was outside the bounds of the array
                .TransformUsing(Transformers.AliasToBean<X>()).List<X>();
    }
}

the results returned have to be grouped with the contents intact (so sql is out since it can only aggregate over the grouped items) but NHibernate can only do this for m,apped entities because there it knows the identity to group by. 返回的结果必须与内容完整分组(因此sql已经输出,因为它只能聚合分组的项目)但是NHibernate只能为m,apped实体执行此操作,因为它知道要分组的标识。 It is simple to do it manually 手动完成很简单

Y yAlias = null;
var tempResults = Session.QueryOver<X>()
     .JoinAlias(x => x.YCollection, () => yAlias, JoinType.LeftOuterJoin)
     .SelectList(list => list
         .Select(() => xAlias.Id)
         .Select(() => xAlias.Name)
         ...
         .Select(() => yAlias.Id)
         ...
     .ToList<object[]>()

List<X> results = new List<X>(100); // switch to Hashset if too slow and order does not matter
foreach(var row in tempResults)
{
    Y y = new Y { Id = (long)row[4], ... };
    X x = results.FirstOrDefault(x => x.Id == (long)row[0]));
    if (x != null)
        x.YCollection.Add(y);
    else
        results.Add(new X { Id = (long)row[0], ..., YCollection = { y } };
}

return results;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM