110,536
社区成员
发帖
与我相关
我的任务
分享
public class UserType
{
public Int32 Id { get; set; }
public string TypeName { get; set; }
}
public class UserInfo
{
public Int32 Id { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public UserType UserType { get; set; }
}
public IList<UserInfo> GetAllUser()
{
IList<UserInfo> list = new List<UserInfo>();
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from UserInfo";
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
UserTypeService userTypeService = new UserTypeService();
UserInfo userInfo = new UserInfo();
userInfo.Id = (Int32)reader["Id"];
userInfo.Name = (string)reader["Name"];
userInfo.Gender = (string)reader["Gender"];
userInfo.UserType = userTypeService.GetUserTypeById((Int32)reader["TypeId"]);//外键的处理
list.Add(userInfo);
}
}
}
return list;
}
var query = from user in userList
select new
{
姓名 = user.Name,
性别 = user.Gender,
类型 = user.UserType.TypeName
};