Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHIbernate: Shortcut for projecting all properties?

I'm trying to generate SQL along the lines of:

SELECT 
  t.*, 
  SELECT (...)
FROM Title t
[trimmed]

using QueryOver

Title title = null;

var q = session
   .QueryOver(() => title)
   .Select(
      Projections.Alias(Projections.Property<Title>(t => t.Id), "Id"),
      Projections.Alias(Projections.Property<Title>(t => t.Name), "Name"),
      ....
      Projections.SubQuery(sq.Where(tt => tt.Id == title.Id))), "TopLevelGenre")
)
[code trimmed]

There are 15 properties in Title that I would like to project. Is there an easier way of doing it so that I don't have to project each property individually as I've started to do above?

like image 209
csano Avatar asked Apr 23 '11 07:04

csano


2 Answers

If you don't mind ICriteria, this works. I have tested with 2.1.2, but I can't see why if wouldn't work with 3.x.

var projectionList = Projections.ProjectionList();
var metadata = session.SessionFactory.GetClassMetadata(typeof(Title));
foreach (var name in metadata.PropertyNames)
{
    projectionList.Add(Projections.Property(name), name);
}
var criteria = DetachedCriteria.For<Title>()
   .SetProjection(projectionList)
   ...;
like image 120
Tim Scott Avatar answered Sep 19 '22 07:09

Tim Scott


I've discovered that as of NHibernate 3.2, this is not possible without manually enumerating all of the properties.

like image 22
csano Avatar answered Sep 23 '22 07:09

csano