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
  • 21,356
  • 13
  • 72
  • 115
user656822
  • 1,147
  • 2
  • 12
  • 29

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
  • 120,221
  • 47
  • 236
  • 329
  • 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