0

我有一个选择linq

如果我必须比较用户名和密码是否允许,但无法访问用户元素。

   var user = from u in db.tbl_User select new {u.UserName,u.Password};
 if(txtname == user && txtpass == pass)
            {
            Form2 frm = new Form2();
            Visible = false;
            frm.Show();

            }
            else
            {

               MessageBox.Show("Error","UserName or Password is wrong",MessageBoxButtons.OK);

            }

我怎样才能给元素user

4

2 回答 2

4

您的user变量是一个查询,它返回一系列匹配的用户 - 而不是单个用户。

听起来您可能想要:

var user = db.tbl_User
             .FirstOrDefault(u => u.UserName == txtname.Text
                                  && u.Password == txtPass.Text);
if (user != null)
{
    ...
}

(顺便说一句,我希望你没有真正的纯文本密码......)

于 2012-07-02T12:44:10.920 回答
0
var user = from u in db.tbl_User select new {u.UserName,u.Password};
if(user.Count() != 1)
    return;
if(txtname == user.UserName && txtpass == pass.Password)
{
        Form2 frm = new Form2();
        Visible = false;
        frm.Show();

        }
        else
        {

           MessageBox.Show("Error","UserName or Password is wrong" ,MessageBoxButtons.OK);

}
于 2012-07-02T12:48:41.827 回答