title: NHibernate - Projections.Property on detached criteria with Alias
body: <p>I am building a semi-complex report query (may not be the best way, but it works fine up until this issue).</p>
  
  <pre><code>// total appointments
  var appts = DetachedCriteria.For&lt;Appointment&gt;("appt")
      .CreateAlias("Lead", "lead")
      .CreateAlias("lead.Promoter", "leadPromoter", JoinType.LeftOuterJoin)
      .Add(Restrictions.EqProperty("leadPromoter.Id", "promoter.Id"))
      .SetProjection(Projections.ProjectionList()
                                     .Add(Projections.CountDistinct("appt.Id"));
  
  // total sales
  var sales = DetachedCriteria.For&lt;Appointment&gt;("sales")
      .CreateAlias("Lead", "lead")
      .CreateAlias("Sale", "sale")
      .CreateAlias("lead.Promoter", "leadPromoter", JoinType.LeftOuterJoin)
      .Add(Restrictions.EqProperty("leadPromoter.Id", "promoter.Id"))
      .SetProjection(Projections.ProjectionList()
                                     .Add(Projections.CountDistinct("sales.Id"));
  
  var projections = Projections.ProjectionList()
      .Add(Projections.SubQuery(appts), "Appointments")
      .Add(Projections.SubQuery(sales), "Sales");
  
  
  var reports = Session.CreateCriteria&lt;Promoter&gt;("promoter")
                  .SetProjection(projections)
                  .SetResultTransformer(Transformers.AliasToBean(typeof(PromoterReportDto)))
                  .List&lt;PromoterReportDto&gt;();
  </code></pre>
  
  <p>This works fine and brings back correct results, however now I need to introduce a where clause on each of the projections (num appointments, num sales etc) on the week day of each projection.</p>
  
  <p>To do this I was adding this to my projections:</p>
  
  <pre><code>// total appointments
  var appts = DetachedCriteria.For&lt;Appointment&gt;("appt")
      .CreateAlias("Lead", "lead")
      .CreateAlias("lead.Promoter", "leadPromoter", JoinType.LeftOuterJoin)
      .Add(Restrictions.EqProperty("leadPromoter.Id", "promoter.Id"))
      .Add(Restrictions.Eq(
                          Projections.SqlFunction("day", NHibernateUtil.DateTime, Projections.Property("appt.AppointmentDate")), selectedDayOfWeek)
                      )
      .SetProjection(Projections.ProjectionList()
                                     .Add(Projections.CountDistinct("appt.Id"));
  
  // total sales
  var sales = DetachedCriteria.For&lt;Appointment&gt;("sales")
      .CreateAlias("Lead", "lead")
      .CreateAlias("Sale", "sale")
      .CreateAlias("lead.Promoter", "leadPromoter", JoinType.LeftOuterJoin)
      .Add(Restrictions.EqProperty("leadPromoter.Id", "promoter.Id"))
      .Add(Restrictions.Eq(
                          Projections.SqlFunction("day", NHibernateUtil.DateTime, Projections.Property("sales.AppointmentDate")), selectedDayOfWeek)
                      )
      .SetProjection(Projections.ProjectionList()
                                     .Add(Projections.CountDistinct("sales.Id"));
  </code></pre>
  
  <p>;</p>
  
  <p>However, it is complaining with this error:</p>
  
  <blockquote>
    <p>NHibernate.QueryException: Could not find property sales.AppointmentDate</p>
  </blockquote>
  
  <p>The property definetly exists, if i remove the alias in the Projections.Property (Projections.Property("AppointmentDate")), it works, however it produces this SQL:</p>
  
  <pre><code>       and datepart(day, this_0_.AppointmentDate) = 0 /* @p4 */
         and datepart(day, this_0_.AppointmentDate) = 0 /* @p5 */
         and datepart(day, this_0_.AppointmentDate) = 0 /* @p6 */
         and datepart(day, this_0_.AppointmentDate) = 0 /* @p7 */
         and datepart(day, this_0_.AppointmentDate) = 0 /* @p8 */
         and datepart(day, this_0_.AppointmentDate) = 0 /* @p9 */
         and datepart(day, this_0_.AppointmentDate) = 0 /* @p10 */) as y0_,
  </code></pre>
  
  <p>As you can see it's using the first instance of my entity, rather than the entity for each specific detached criteria.</p>
  
  <p>Sorry for the long question, i'm not quite sure how to explain the issue without all the code etc.</p>
  
  <p>I can paste up some more code / SQL if needed.</p>
  
  <p>Paul</p>