1 直接使用表对应的实例属性值 ,如本例中的RoleUserInfo的属性UserInfo
IEnumerable <RoleUserInfo > roleUserInfos= db.RoleUserInfoes.Where(p => p.Role_ID == roleID).AsEnumerable <RoleUserInfo >();
return from u in roleUserInfos
select new UserInfo { ID = u.UserInfo.ID, PWD = u.UserInfo.PWD, UName = u.UserInfo.UName };
2 自己写个存储过程,存储过程 GetUsersByRoleID对应的实例是GetUsersByRoleID_Result,在继承了dbContext的类里还会生成一个方法名为GetUsersByRoleID与之对应。
继承了dbContext的类中对应存储过程的方法,返回类型为objectResult
public virtual System.Data.Entity.Core.Objects.ObjectResult<GetUsersByRoleID_Result> GetUsersByRoleID(Nullable<int> roleID)
{
var roleIDParameter = roleID.HasValue ?
new System.Data.Entity.Core.Objects.ObjectParameter("RoleID", roleID) :
new System.Data.Entity.Core.Objects.ObjectParameter("RoleID", typeof(int));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<GetUsersByRoleID_Result>("GetUsersByRoleID", roleIDParameter);
}
return from p in db.GetUsersByRoleID(roleID) as IEnumerable<GetUsersByRoleID_Result>
select new UserInfo { ID = p.ID, PWD = p.PWD, UName = p.UName };