NHibernateWhy do we need to specify the query type twice?

time to read 1 min | 171 words

Why do we have to do something like that?
var blogs = s.CreateCriteria<Blog>()
    .Add(Restrictions.Eq("Title", "Ayende @ Rahien"))
    .List<Blog>();

We have to specify Blog twice, isn’t that redundant?

Yes and no. The problem is that we can’t assume that we are going to return instances of Blog. For example:

var blogs = s.CreateCriteria<Blog>()
    .Add(Restrictions.Eq("Title", "Ayende @ Rahien"))
    .SetProjection(Projections.Id())
    .List<int>();

More posts in "NHibernate" series:

  1. (19 Nov 2010) Complex relationships
  2. (27 Jun 2010) Streaming large result sets
  3. (25 May 2009) Why do we need to specify the query type twice?
  4. (20 Mar 2009) Avoid identity generator when possible
  5. (26 Mar 2007) Nullable DateTime Issues
  6. (08 Jan 2007) Fetching multiply collections in one roundtrip