O'Reilly logo

NHibernate 4.x Cookbook - Second Edition by Jason Dentler, Alexander Zaytsev, Gunnar Liljas

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Eager loading with LINQ

Often, when we query for some set of entities, we also want to load some of the related entities. In this recipe, we'll show you how we can use LINQ extensions to eager load the child collections of our query results.

Getting ready

Complete the Getting Ready section at the beginning of this chapter.

How to do it…

  1. Create a new folder named EagerLoadingWithLinq in the project.
  2. Add a new class named Recipe to the folder:
    using System.Linq; using NH4CookbookHelpers.Queries; using NH4CookbookHelpers.Queries.Model; using NHibernate; using NHibernate.Linq; namespace QueryRecipes.EagerLoadingWithLinq { public class Recipe : QueryRecipe { protected override void Run(ISession session) { var book = session.Query<Book>() .Fetch(x => x.Publisher) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required