1
votes

I having some trouble with a Linq NHibernate query.

I need to get a batch of vouchers with their details. As I need to iterate them, I wanted to get all the information in a single execution.

My query is the following one:

                return this.Session.Query<VouchersToIntegrate>()
                .Take(query.BatchSize)
                .Fetch(x => x.VoucherLines)                    
                .ToList();

Where VouchersToIntegrate is the voucher and VoucherLines the lines of each voucher.

The batchSize is set to 50.000 now, but when I return all the objects, I just get 23XXX. This is because I guess the framework is internally executing a distinct after it gets all the objects from the database (client-side). Is there any other way to get the 50.000 objects with the distinct filter in the SQL server side?

Thanks

1
Iterating over lazy loaded properties with NHibernate does not cause n+1 loading issues, if you have mapped your entities properly. See this more in depth explanation. So if you want to eager load because you have the n+1 loading issue, you should read it.Frédéric

1 Answers

0
votes

There is not a way to construct this query with NHibernate's LINQ provider.

You can, however, construct the appropriate SQL query by hand and then map it to entities using NHibernate's "Native SQL" API: http://nhibernate.info/doc/nhibernate-reference/querysql.html.