O'Reilly logo

NHibernate 3.0 Cookbook by Jason Dentler

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

Using QueryOver projections and aggregates

In some cases, we only need specific properties of an entity. In other cases, we may need the results of an aggregate function, such as average or count. In this recipe, I'll show you how to write QueryOver queries with projections and aggregates.

How to do it...

  1. Complete the setup steps in the introduction at the beginning of this chapter.
  2. Add the following method to the Queries class.
    public IEnumerable<NameAndPrice> GetMoviePriceList()
    {
      return _session.QueryOver<Movie>()
        .Select(m => m.Name, m => m.UnitPrice)
        .List<object[]>()
        .Select(props =>
          new NameAndPrice()
          {
            Name = (string)props[0],
            Price = (decimal)props[1]
          });
    }
  3. Add the following method to Queries to fetch a simple average movie price:
    public decimal ...

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