nHibernate Criteria API Projections

2019-08-24 11:28发布

I have an entity that is like this

public class Customer 
{
    public Customer() { Addresses = new List<Address>(); }
    public int CustomerId { get; set; }
    public string Name { get; set; }
    public IList<Address> Addresses { get; set; }
}

And I am trying to query it using the Criteria API like this.

ICriteria query = m_CustomerRepository.Query()
    .CreateAlias("Address", "a", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
var result = query
  .SetProjection(Projections.Distinct(
    Projections.ProjectionList()
      .Add(Projections.Alias(Projections.Property("CustomerId"), "CustomerId"))
      .Add(Projections.Alias(Projections.Property("Name"), "Name"))
      .Add(Projections.Alias(Projections.Property("Addresses"), "Addresses"))
    ))
  .SetResultTransformer(new AliasToBeanResultTransformer(typeof(Customer)))
  .List<Customer>() as List<Customer>;

When I run this query the Addresses property of the Customer object is null. Is there anyway to add a projection for this List property?

1条回答
做个烂人
2楼-- · 2019-08-24 12:01

The code seems to be good. So the problem can be in the mapping for the Addresses property.

查看更多
登录 后发表回答