nHibernate Criteria API预测

时间:2009-06-24 02:04:55

标签: c# nhibernate orm icriteria

我有一个像这样的实体

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; }
}

我正在尝试使用Criteria API这样查询它。

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>;

当我运行此查询时,Customer对象的Addresses属性为null。无论如何都要为此List属性添加投影吗?

1 个答案:

答案 0 :(得分:0)

代码似乎很好。所以问题可能出在Addresses属性的映射中。