5

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
Samuel Goldenbaum
  • 18,391
  • 17
  • 66
  • 104

2 Answers2

3

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

Samuel Goldenbaum
  • 18,391
  • 17
  • 66
  • 104
2

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[]>();
Martin Ernst
  • 5,629
  • 2
  • 17
  • 14