如何做到这一点:
var tmpUser = System.Web.HttpContext.Current.User.Identity;
这会返回用户登录的内容,例如当我登录时,它返回用户名,所以身份就是例如daniel(用户名)
现在以其他形式,我想返回此用户名的ID,所以我从这开始:
var db = new userDbEntities(); // this is my connection to Database
然后:
var connectedUser = from user in db.UserTable
select user
现在我需要WHERE句子,我可以检查USERNAME是否等于tmpUser,然后在UserTable中返回该用户的ID。
感谢任何想法...
答案 0 :(得分:1)
假设 UserName 是 UserTable 中的列名,这种方式可行:
string tmpUser = System.Web.HttpContext.Current.User.Identity.Name;
using(var db = new userDbEntities())
{
var connectedUser = (from user in db.UserTable
where user.UserName == tmpUser
select user).FirstOrDefault();
}
或者您只需使用SingleOrDefault:
string tmpUser = System.Web.HttpContext.Current.User.Identity.Name;
using(var db = new userDbEntities())
{
var connectedUser = db.UserTable.SingleOrDefault(x=>x.UserName == tmpUser);
}