Question

How to do this:

var tmpUser = System.Web.HttpContext.Current.User.Identity;

This returns me what user is logged in, e.g. it returns, when I am login, the name of username, so Identity is e.g. daniel (username)

Now in some other form, I want to return the ID of this username, so I have start with this:

var db = new userDbEntities(); // this is my connection to Database

Then:

var connectedUser = from user in db.UserTable 
                    select user  

now here I need WHERE sentence, where I can check if USERNAME is equal to tmpUser, then return the ID of this user in UserTable.

Thanks for any ideas...

Was it helpful?

Solution

Assuming UserName is the column name in UserTable this way will work:

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();
}

or you can simply do using SingleOrDefault:

string tmpUser = System.Web.HttpContext.Current.User.Identity.Name;

using(var db = new userDbEntities())
{
    var connectedUser  = db.UserTable.SingleOrDefault(x=>x.UserName == tmpUser);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top