Search code examples
c#linq

How to get the value of a specific column? I'm executing a SQL query in linq using C#


I am trying to get the value of my column authID so I can authenticate the user authority in my login form. Please help me I'm new to Linq. Thank you.

This is my code

acpEntities db = new acpEntities();

public void authenticateUser()
{
    var sqlQuery = db.user_tbl.SqlQuery("OPEN SYMMETRIC KEY
encryptUserPass DECRYPTION BY PASSWORD = 'pa$$w0rd'; SELECT userID, username, pass = CAST(DECRYPTBYKEY(pass) AS nvarchar), authID, RID, isActive, timestamp, transDate FROM user_tbl WHERE username = @username AND CAST(DECRYPTBYKEY(pass) AS nvarchar) = @pass", 
                                        new System.Data.SqlClient.SqlParameter("@username", txtUsername.Text), 
                                        new System.Data.SqlClient.SqlParameter("@pass", txtPassword.Text))
                                      .ToList();

    if (sqlQuery.Any())
    {
        frMain main = new frMain();
        main.Show();
        this.Hide();
    }
    else
    {
        MessageBox.Show("Invalid account, please contact your administrator", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
}

private void btnLogin_Click(object sender, EventArgs e)
{
    authenticateUser();
}

The code is working, I just don't know how to get values to a specific column using this sqlQuery to execute a query in Linq using C#. I've been searching on Google and Youtube by I can't find any answers


Solution

  • I got it. string authority = sqlQuery.FirstOrDefault().authID;