Samuel Goldenbaum
Samuel Goldenbaum

Reputation: 18909

NHibernate Projections - how to project collections

Have a scenario where I need to only select a single/few columns from an entity, but multiple children in a query. I have been trying with projections but getting an error on the collections property. This is such a normal situation, yet cannot find info on projecting collections - only properties.

Customer customerAlias = null;
Order orderAlias = null;
 var list = _session.QueryOver<Customer>(() => customerAlias)
                    .JoinAlias(x => x.Orders, () => orderAlias, JoinType.LeftOuterJoin)
                    .Select(
                       Projections.Property(() => customerAlias.Name),
                       Projections.Property(() => customerAlias.Orders))//this is the issue
                   .List<object>();

Error returned is:

System.IndexOutOfRangeException : Index was outside the bounds of the array

Upvotes: 5

Views: 2511

Answers (2)

Samuel Goldenbaum
Samuel Goldenbaum

Reputation: 18909

Cannot be done in NH 3.3. https://nhibernate.jira.com/browse/NH-3176

Upvotes: 3

Martin Ernst
Martin Ernst

Reputation: 5679

How about reversing the query (assuming that Order has a Customer property):

var list = _session.QueryOver<Order>()
                .Select(
                   Projections.Property(o => o.Customer.Name),
                   Projections.Property(o => o.OrderProperty1),
                   Projections.Property(o => o.OrderProperty2)) // etc..
               .List<object[]>();

Upvotes: 2

Related Questions