C# 如何使用NHibernate从数据库检索特定字段?
我一直在尝试使用NHinernate从数据库中检索特定字段。问题是我的用户列表总是返回null 我该怎么办?这是我的密码:C# 如何使用NHibernate从数据库检索特定字段?,c#,asp.net,nhibernate,C#,Asp.net,Nhibernate,我一直在尝试使用NHinernate从数据库中检索特定字段。问题是我的用户列表总是返回null 我该怎么办?这是我的密码: public IList<Users> GetHospitalStaff(string name) { ISession session = NHibernateHelper.OpenSession(); ICriteria crt = session.CreateCriteria<Users>(); crt.Ad
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;
}
public IList GetHospitalStaff(字符串名称){
ISession session=NHibernateHelper.OpenSession();
ICriteria crt=session.CreateCriteria();
crt.Add(Restrictions.Eq(“HospitalName”,name));
添加(!Restrictions.Eq(“角色”、“管理员”);
IList users=crt.SetProjection(Projections.ProjectionList()
.Add(Projections.Property(“名称”))
.Add(Projections.Property(“电子邮件”))
.Add(投影、属性(“电话”))
.Add(Projections.Property(“出生日期”))
.Add(Projections.Property(“HospitalName”))
.Add(Projections.Property(“角色”))
.SetResultTransformer(新的NHibernate.Transform.AliasToBeanResultTransformer(用户类型)))
.List();
session.Close();
返回用户;
}
您可能必须使用Projection.Alias()
:
将您的字段映射回
或者只需指定.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")
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")