Using LINQ to NHibernate

In the previous chapter, the ICritera API, a programmatic method of data retrieval, was discussed. Recall that the ICriteria interface contains the QueryOver API, which is a type-safe compile-time wrapper for the ICriteria API. It is possible to use lambda expressions in both QueryOver and LINQ to NHibernate. In this chapter, you will learn the following:

  • Working with LINQ to NHibernate
  • Using LINQ to NHibernate with lambda expressions
  • Implementing paging using LINQ to NHibernate
  • Understanding aggregate database functions with LINQ to NHibernate


The release of NHibernate 3.0 included LINQ capabilities for use with NHibernate. LINQ is a very powerful querying tool that exists for objects, SQL, XML, and many other extensions for querying information sources. The LINQ to NHibernate provider implements most of the .NET LINQ operators found in the System.Linq namespace. Table 4-1 shows the operator types and execution method(s) provided with the LINQ to NHibernate provider.

TABLE 4-1:LINQ Operators

Aggregates Count, Sum, Min, Max, and Average
Conversion ToArray, ToList, ToDictionary, and OfType
Element First, FirstOrDefault, ElementAt
Generation Range, and Repeat
Grouping GroupBy
Join Cross, Group, and Left Outer
Ordering OrderBy, OrderByDescending, ThenBy, ThenByDescending, and Reverse
Partitioning Take, Skip, TakeWhile, and SkipWhile
Projection Select and SelectMany
Quantifiers Any and ...

Get Working with NHibernate 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.