我懒加载集合,也因为人员表中有很多字段,我正在编写一个投影函数来仅检索某些属性。它适用于属性,而不是其他实体的集合。如果它们作为代理加载我会很好,我可以稍后再获取它们,但现在它只是加载为空。
public IList<Person> ListTop40()
{
var list = _session.CreateCriteria(typeof(Person))
.SetProjection(Projections.ProjectionList()
.Add(Projections.Property("FirstName"))
.Add(Projections.Property("LastName"))
.Add(Projections.Property("Jersey"))
.Add(Projections.Property("FortyYard"))
.Add(Projections.Property("BenchReps"))
.Add(Projections.Property("VertJump"))
.Add(Projections.Property("ProShuttle"))
.Add(Projections.Property("LongJump"))
.Add(Projections.Property("PersonSchoolCollection"))
)
.List<IList>()
.Select(l => new Person() { FirstName = (string)l[0], LastName = (string)l[1], Jersey = (Decimal)l[2], FortyYard = (Decimal)l[3], BenchReps = (Decimal)l[4], VertJump = (Decimal)l[5], ProShuttle = (Decimal)l[6], LongJump = (Decimal)l[7], PersonSchoolCollection = (IList<Person_School>)l[8]});
IList<Person> s = list.ToList();
return s;
}