Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 操作员'==';不能应用于字符串和字符类型的操作数_C#_String_Char_Type Conversion - Fatal编程技术网

C# 操作员'==';不能应用于字符串和字符类型的操作数

C# 操作员'==';不能应用于字符串和字符类型的操作数,c#,string,char,type-conversion,C#,String,Char,Type Conversion,我正在使用char,但我感到困惑,因为我知道我必须将这个char转换为string数据类型 else if (mode.Equals("Update")) { char id = char.Parse(txtuserId.Text.ToString()); //conversion as i told before string newName = txtname.Text; string newemail

我正在使用
char
,但我感到困惑,因为我知道我必须将这个
char
转换为
string
数据类型

else if (mode.Equals("Update"))
        {
            char id = char.Parse(txtuserId.Text.ToString()); //conversion as i told before

            string newName = txtname.Text;
            string newemail = txtemail.Text;
            string newpassword = txtpassword.Text;
            string newaddress = rtbaddress.Text;
            string newphoneNumber = txtphoneNumber.Text;
            string newroleName = cmbrole.Text;

            users updatedusers = (
                from x in db.users
                where x.id==id // THE WRONG ONE
                select x
                ).First();
        }

可以解析字符串中的字符,但不能将具有多个字符的字符串解析为字符。这应该是一个个案的基础上的情况

如果txtoserid是一个文本框,其长度仅限于一个字符,则可以使用

char id = char.Parse(txtuserId.Text.ToString());`

如果txtuserID不限于一个字符,则只能使用以下代码: (通过索引获取所需的字符)


但说真的,我想这不是你需要的。您的代码中有多余的代码。如果字符串是代码底部类似SQL的代码中所需的内容,则不要将其转换为字符数组,然后将其转换为字符串。我建议你:

else if (mode.Equals("Update"))
        {
            String id= txtuserId.Text; // change this part

            string newName = txtname.Text;
            string newemail = txtemail.Text;
            string newpassword = txtpassword.Text;
            string newaddress = rtbaddress.Text;
            string newphoneNumber = txtphoneNumber.Text;
            string newroleName = cmbrole.Text;

            users updatedusers = (
                from x in db.users
                where x.id==id // leave it as it is
                select x
                ).First();
        }

在这段代码中,您不会感到困惑,因为字符串没有被重新转换,而是被正确地放置在它应该位于的位置。

我根据注释回答这个问题

@不幸运:数据类型为char(5)

这意味着列能够存储长度为5的字符数组,现在考虑输入,即“代码> TXTUSERID.Text < /代码>。它本身就是一个字符串,因为我们已经知道字符串是一个字符数组,所以您不需要使用

char.Parse来转换它,您可以直接比较它们。因此,在这种情况下,where子句应该是
where x.id==txtoserid.Text

附加说明: 您不需要使用
char.Parse
从字符串中获取字符,而是可以使用索引从字符串的特定位置获取字符。假设你想从一个字符串中获取第一个字符,在这种情况下,你可以使用如下:

if(string.IsNullOrEmpty(txtuserId.Text))
  char firstChar = txtuserId.Text[0];

尝试在x.idstring id=txtuserId.Text中添加.tostring;表
users
@earvinillcastillo:where x.id.ToString=id中
id
列的类型是什么?仍然错误..@ManishSingh如果我使用string id=txtuserId.Text,表上的数据必须是string。但是要求是char.谢谢你的建议
if(string.IsNullOrEmpty(txtuserId.Text))
  char firstChar = txtuserId.Text[0];