联表查询

it2022-05-05  127

首先,我们在数据库里看一下这些表的关系: 查询的代码是这样的: var listUser = (from tbUser in myModel.S_User join tbUserTypeDetail in myModel.R_UserTypeDetail on tbUser.UserID equals tbUserTypeDetail.UserID join tbUserType in myModel.S_UserType on tbUserTypeDetail.UserTypeID equals tbUserType.UserTypeID select new { UserID = tbUser.UserID, UserName = tbUser.UserName, UserCode = tbUser.UserCode, Password = tbUser.Password, UserTypeID = tbUserType.UserTypeID, UserType = tbUserType.UserType, typeClass = tbUserType.typeClass }).ToList(); 上面的“tb**”是范围变量(自定义表名)之后的就是查询的表名 “equals”使前后的两个表的主键和外键相等。 “select new{}”中括号里就是你需要查询的字段。


最新回复(0)