title: nhibernate 2 linq eager load
body: <p>I'm starting out with nHibernate and have a simple example that I cannot get working as I'd like.</p>
  
  <p>I have two model objects (Blog and Posts) and I would like to load them all in a single query for one scenario.  I want lazy loading in other cases.</p>
  
  <p>I naively thought that I could write something like this:</p>
  
  <pre><code>var blogs = session.Linq&lt;Blog&gt;().Expand("Posts");
  </code></pre>
  
  <p>But this will give me an instance of blog for every post rather than adding the posts to the blog.</p>
  
  <p>I know I'm doing something stupid.  Can someone please point out what it is?  Is it that I need to relate the post and blog entities in my linq query?</p>
  
  <p>Code and Mappings:</p>
  
  <pre><code>public class Blog
  {
      public Blog()
      {
          Posts = new HashSet&lt;Post&gt;();
      }
      public virtual long Identifier { get; set; }
      public virtual string Name { get; set; }
      public virtual ICollection&lt;Post&gt; Posts { get; set; }
      public virtual Post AddPost(Post post)
      {
          post.Blog = this;
          Posts.Add(post);
          return post;
      }
  }
  
  
  public class Post
  {
      public virtual long Identifier { get; set; }
      public virtual string Name { get; set; }
      public virtual Blog Blog { get; set; }
  }
  
  &lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="nhibEx" namespace="nhibEx"&gt;
    &lt;class name="Blog" lazy="true"&gt;
      &lt;id name="Identifier"&gt;
        &lt;generator class="native" /&gt;
      &lt;/id&gt;
      &lt;property name="Name" not-null="true" length="100"/&gt;
      &lt;set name="Posts" inverse="true" cascade="save-update" lazy="true"&gt;
        &lt;key column="BlogIdentifier" foreign-key="fk_Post_Blog"/&gt;
        &lt;one-to-many class="Post"/&gt;
      &lt;/set&gt;
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  
  &lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="nhibEx" namespace="nhibEx"&gt;
    &lt;class name="Post" lazy="true"&gt;
      &lt;id name="Identifier"&gt;
        &lt;generator class="native" /&gt;
      &lt;/id&gt;
      &lt;property name="Name" not-null="true" length="255"/&gt;
      &lt;many-to-one name="Blog" column="BlogIdentifier" class="Blog" /&gt;
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>