2

How can I get same result with NHibernate QueryOver when using entity framework linq like this.

var result = items
   .include("subEntity1")
   .include("subEntity2")
   .include("subEntity3")
   .where(...).skip(x).take(y); 
Rippo
  • 22,117
  • 14
  • 78
  • 117
user656822
  • 1,167
  • 2
  • 12
  • 30

1 Answers1

3

A syntax with QueryOver could look like this:

var query = session.QueryOver<MyEntity>()
    // force the collection inclusion
    .Fetch(x => x.Collection1).Eager
    .Fetch(x => x.Collection2).Eager
    ...
    // force the relations inclusion
    .Fetch(x => x.SubEntity1).Eager
    .Fetch(x => x.SubEntity2).Eager
    ...
    // paging
    .Skip(x)
    .Take(y);

var list = query
     .List<MyEntity>();

Sources:

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • 1
    I try this one but I'm getting duplicate entity in the result – user656822 Feb 24 '14 at 10:58
  • Yes, that's the impact of the Fetch - collection. It always results in carthesian product. The appropriate way is in fact to do **NOT** Fetch. Change your mapping to a use batch-size, which will end in more then 1 query, but still very efficient. Please, check http://stackoverflow.com/a/20970816/1679310 or http://stackoverflow.com/a/19287008/1679310 – Radim Köhler Feb 24 '14 at 11:04