我一直在尝试使用NHinernate从我的数据库中检索特定字段。问题是我的用户列表一直返回null。
我该怎么办?这是我的代码:
public IList<Users> GetHospitalStaff(string name) {
ISession session = NHibernateHelper.OpenSession();
ICriteria crt = session.CreateCriteria<Users>();
crt.Add(Restrictions.Eq("HospitalName", name));
crt.Add(!Restrictions.Eq("Role", "admin"));
IList<Users> users = crt.SetProjection(Projections.ProjectionList()
.Add(Projections.Property("Name"))
.Add(Projections.Property("Email"))
.Add(Projections.Property("Telephone"))
.Add(Projections.Property("DateOfBirth"))
.Add(Projections.Property("HospitalName"))
.Add(Projections.Property("Role")))
.SetResultTransformer(new NHibernate.Transform.AliasToBeanResultTransformer(typeof(Users)))
.List<Users>();
session.Close();
return users;
}
答案 0 :(得分:0)
可能您必须使用Projection.Alias()
:
Projections.ProjectionList()
.Add(Projections.Alias(Projections.Property("Name"), "Name"))
.Add(Projections.Alias(Projections.Property("Email"), "Email"))
.Add(Projections.Alias(Projections.Property("Telephone"), "Telephone"))
.Add(Projections.Alias(Projections.Property("DateOfBirth"), "DateOfBirth"))
.Add(Projections.Alias(Projections.Property("HospitalName"), "HospitalName"))
.Add(Projections.Alias(Projections.Property("Role"), "Role"))
将您的字段映射回来。
或者只指定.Add()
方法的第二个参数:
Projections.ProjectionList()
.Add(Projections.Property("Name"), "Name")
.Add(Projections.Property("Email"), "Email")
.Add(Projections.Property("Telephone"), "Telephone")
.Add(Projections.Property("DateOfBirth"), "DateOfBirth")
.Add(Projections.Property("HospitalName"), "HospitalName")
.Add(Projections.Property("Role"), "Role")